kflorence/jquery-deserialize

Support CakePHP checkbox

Closed this issue · 1 comments

When using the CakePHP framework and their FormHelper you see that when creating checkboxes it produces the following HTML:

<input type="hidden" name="data[Post][Published]" id="PostPublished_" value="0" />
<input type="checkbox" name="data[Post][Published]" value="1" id="PostPublished" />

When a form is submitted, the server receives data[Post][Published] as false when the checkbox is unchecked. The "problem" with deserialize() it will skip/ignore the actual checkbox. I've created a specific solution for this CakePHP form problem (insert at line 106):

// Exception for CakePHP framework
if ( type == "hidden" && element.length == 2 && element[ 1 ].type == "checkbox" && element[ 0 ].id == element[ 1 ].id + '_') {
    // The hidden element should be unchanged
    type = "checkbox";
}

1: Check if the current type is hidden.
2: Check if the element array has length 2.
3: Make sure the second element is the checkbox.
4: Compare the element ids (with underscore).

When these conditions are true, the type should be "checkbox" so the hidden value stays the same but the actual checkbox is handled by serialize().

Hey @dschraven, thanks for this, however I don't want to have edgecases for various frameworks in the code. In this case, I think the proper fix would be to call deserialize() normally and apply these modifications via the change option (or even after deserialization) to accommodate the way CakePHP handles forms.