Repopulating checkboxes in Codeigniter after Unsuccessful Form Validation

In order for set_checkbox to work properly, an actual validation rule needs to be used on that element. I ran into this problem and could not get the value to show on a resubmit until I included:

$this->form_validation->set_rules('checkbox_name', 'checkbox_title', 'trim');

Then, everything worked perfect.


You set_checkbox calls are wrong. When you're using an array like "ambience[]" in form_checkbox, you don't want to include the square brackets ([]) in your set_checkbox call. The other problem is that set_checkbox requires a second parameter which you've only included in the first 2 checkboxes.

The set_checkbox should always be like this:

set_checkbox('ambience', 'value');

Where 'value' is the second parameter of the form_checkbox call. Like this:

form_checkbox('ambience[]', 'value', set_checkbox('ambience', 'value'));

I actually found that it only works if you use like this:

form_checkbox('ambience[]', 'value', set_checkbox('ambience[]', 'value'));

You need the square array brackets on the name for it to work properly.