I have a problem repopulating a set of checkboxes after an unsuccessful form validation returns the user back to the same form. Dropdown menus and text inputs could be repopulat
Here's a working example. You are required to include the array name with brackets [] in each of $this->form_validation->set_rules()
, form_checkbox()
and set_checkbox()
.
In the controller:
$this->load->library('form_validation');
$this->form_validation->set_rules('set_reminder_days[]', 'Reminder Day', 'trim');
if( $this->form_validation->run() == FALSE ) //Field validation failed.
{
//form validation errors will show up automatically
}
else //Validation success.
{
//This is an array of all checked checkboxes
$reminder_days = $this->input->post('set_reminder_days');
}
In the view:
$day_options = array(
'S' => 'Sunday',
'M' => 'Monday',
'T' => 'Tuesday',
'W' => 'Wednesday',
'Th' => 'Thursday',
'F' => 'Friday',
'Sa' => 'Saturday'
);
foreach( $day_options as $key => $day_option ) {
echo form_checkbox('set_reminder_days[]', $key, set_checkbox('set_reminder_days[]', $key), 'class="form-checkbox"');
}