New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
checkboxUncheckedValue
and arrays
#25
Comments
I am assuming that you are checking the "input2" checkbox and then serializing with something like: $('input').serializeJSON(checkboxUncheckedValue: "false", parseBooleans: true); I agree the expected result should be an array, not a hash. But with the checkboxUncheckedValue option, the "false" values should be included. Expected result with only "input2" checked: {
"flags": [false, "input2"]
} Don't you agree with this? |
Hmm. In terms of Let's say you do a HTTP PATCH request to update which |
I have to think about it. |
If you do an update of an array of strings you pass in the whole lot, as there's no way of distinguishing what is removed otherwise. You can solve this with an object but it's not really RESTful. Also, since you also need to know if all the entries has been removed you also need to pass in an empty array. This should be the role of |
.. but obviously you don't have to agree to me :) |
You know KATT, there's a blur line between more features and simplicity. If a problem becomes to complex it is probably better to not support it and let users roll their own solution. There is a clear case here where the array of checkboxes is serialized as an object. That needs to be fixed. But if I'm not sure about how to easily and clearly solve the problem, I prefer to recommend some workaround meanwhile and put a solution later when I'm convinced. There is value in what you said, and I will probably go with it. It just still doesn't click the relation between the option and the behavior. Time will say ... |
Not a very elegant solution.. but at least I added some tests too. Please code review dc74490 and get back to me what you think. |
@marioizquierdo, have you had time to think about it more? |
I'm thinking ... the option But there's value on sending an empty array or including only selected values. <input type="hidden" name="flags" value="[]"> <!-- ensure always send a list -->
<input type="checkbox" name="flags[]" value="input1">
<input type="checkbox" name="flags[]" value="input2"> var parseEmpryArray = function(val){ return val == "[]" ? [] : val; };
$('input').serializeJSON({parseWithFunction: parseEmpryArray }); Otherwise, using the defaults and checking for existence of I will get into the code this weekend. Thanks! |
It was a good idea, only it don't work to do that (which was obvious when I think about it).
|
Yes it works. But you need no forget about the I know it's still not an easy way to send an empty array.. I wish the name syntax would allow for something simpler. |
The code at dc74490 is good, specially because is baked by tests (KATT@a0be870). Unfortunately I'm not merging it because I decided not to implement this functionality after all. I prefer to keep around a well-known problem than introducing new hidden complications. |
Interesting I was trying to reproduce the bug and I couldn't. It is returning a list for me. With the expected behavior that I mentioned. describe('checkboxUncheckedValue', function() {
it('works on a list of checkboxes', function() {
$form = $('<form>' +
'<label class="checkbox-inline">' +
' <input type="checkbox" name="flags[]" value="input1"> Input 1' +
'</label>' +
'<label class="checkbox-inline">' +
' <input type="checkbox" name="flags[]" value="input2"> Input 2' +
'</label>' +
'</form>');
obj = $form.serializeJSON({checkboxUncheckedValue: 'false'});
expect(obj).toEqual({
'flags': ['false', 'false']
});
$form.find('input[value="input1"]').prop('checked', true);
obj = $form.serializeJSON({checkboxUncheckedValue: 'false'});
expect(obj).toEqual({
'flags': ['input1', 'false']
});
});
}); Are you sure you are not running an old version of Zepto or jQuery? |
Note that from version <input type="hidden" name="flags:array" value="[]"> <!-- send empty list if all checkboxes are unchecked -->
<input type="checkbox" name="flags[]" value="input1">
<input type="checkbox" name="flags[]" value="input2"> $('input').serializeJSON(); // <-- no options needed |
I ran into an issue with combining
checkboxUncheckedValue
and array-like checkboxesIt's currently getting treated as hash, i.e.
It would make sense if they were defined as
name="flags[input1]"
but in this case it should rather be parsed as an array.Expected result
No checked boxes:
{ "flags": [] }
Checked boxes:
The text was updated successfully, but these errors were encountered: