EllisLab/CodeIgniter — Issue #949: Checkbox setting ignored if Form validation in use and checkbox not being validated.
Author: AvalonNZ
Created: 2012-01-22 06:28:28+00:00
State: open

I have a registration form with multiple fields on it including two checkboxes. Most of the fields are validated (they're required) but I have a ONE checkbox that is optional and doesn't need to be validated so no rules are set for it.

When there's an error on the form I redisplay it and every field is redisplayed with the value set by the user EXCEPT the checkbox that wasn't validated.

Investigation in the library code suggests that the problem is due to the fact that there are two set_checkbox functions: one in form_helper.php which is globally available and one as a method in form_validation.php. The global set_checkbox function checks to see if the form_validator object is available and if so just hands control to it even if the field in question is NOT being validated. And since the form_validation object only contains POST information about fields that are being validated, the original value of any field (such as my checkbox) that has no validation rule is lost.

I've fixed the problem by altering the library code for the form validator and it's attached to this message. However it's pretty quick and dirty and someone who is more familiar with the form validation code might find a better solution.

I got around the problem by simply dumping the logic from the global set_checkbox function (which reads the $_POST array directly) into the section of the set_checkbox method that deals with the field not being found in the _field_data array.