macek/jquery-serialize-object

Missing empty values

Opened this issue · 3 comments

How to reproduce
Use the following demo - http://jsfiddle.net/9xfdvLbL/

Conditions
Typing some copy in the input text and by selecting one or more categories, you get:

{"filtering":{"title":"john doe","categories":["1","3"]}}

Problem
When submitting the form without entering any values in the form fields. You get:

{"filtering":{"title":""}}

Expectation
When submitting the form with no values in the fields to get:

{"filtering":{"title":"","categories":[]}}

This is a current problem with jQuery.serializeArray

Using your code provided in your fiddle, the value of the text input is an empty string

$("input[type=text]").val()
// => ""

And the value of the select is null

$("select").val();
// => null

Now the issue with this is how jQuery.serialize is implemented. null values will be filtered out, but empty strings will not. That explains why the input value is included in the result but the select value is not.


I'm currently working on the 3.x version of this plugin that will no longer depend on the problematic jQuery.serializeArray. It has been a sore spot for many of the features we wish to support and so it's going to be replaced.

However, this may or may not solve your problem depending on how it gets implemented. 3.x's goal is to implement the W3C HTML JSON form submission spec as discussed in #24. How we choose to serialize empty values is yet to be decided.

I'll follow up here with any progress we make on this issue.

I am having an issue with names like this not being recognized.

seatmap_options[calendar][g_1-8][1-8_extra5]

Any ideas what's wrong? The fields are just ignored

The fields aren't validating for whatever reason. Not sure if it's a bug or not, or if the jquery plugin doesn't like my overly complex array in the name attribute.

I was able to comment out the validation line and it works fine now. Thank you for this. I wrote a simpler version years ago that didn't work as well with 3d arrays. This one does.