I have a problem with my form validation rules (in a seperate config file). This occurs with checkboxes.
To organize my validation rules, I created a library file called validation_rules
. This library contains all my custom callbacks like valid_date
etc. To be able to call these rules, I load the library, and then use the following config:
array(
'field' => 'terms',
'label' => 'lang:reg_lbl_terms',
'rules' => array(array('terms_accepted', array($ci->validation_rules, 'terms_accepted')))
),
Where $ci
is a reference to CodeIgniter ($this
).
Now this works fine for most types of input, but it doesn't work for checkboxes that are left empty, probably since they don't get posted.
HOWEVER, when I ditch my library and simply add the callback to the controller, everything works fine with the following config:
array(
'field' => 'terms',
'label' => 'lang:reg_lbl_terms',
'rules' => array('callback_terms_accepted')
),
Also, when I add the rules required
anywhere in the rules
array (or string), the required
rule does get called (returning false since the checkbox is not checked), but all other rules are completely ignored.
This must be a bug in CodeIgniter right? Does anybody have a solution or workaround? Of course this is an option, but I really don't like it.
Relevant documentation: http://www.codeigniter.com/user_guide/libraries/form_validation.html
Edit: Checkbox PHP/HTML:
<?php
$data = array(
'name' => 'terms',
'value' => 'true'
);
echo form_checkbox($data);
// Results in: <input type="checkbox" name="terms" value="true">
// No set_value() is used.
?>