I have a problem with a form validation in CI. The rule seems does not call the function My code is the following:
[...]
$this->form_validation->set_rules('last_name', 'last name','trim|required|min_length[3]|max_length[30]|callback_reserved',
[
'required' => 'The %s is missed',
'min_length' => 'The %s must contain at least %s letters',
'max_length' => 'The %s must contain at max %s letters',
]);
[...]
function reserved($str) {
$reserved = ['aaaa','bbbb','cccc','dddd'];
if (in_array(strtolower($str), $reserved)) {
$this->form_validation->set_message('reserved', 'The {field} '.$str.' is reserved');
return false;
} else {
return true;
}
}
Here is a better way to create a custom validation rule:
$config = array(
'field' => 'last_name',
'label' => 'last name',
'rules' => array('trim', 'required', array('last_name_is_reserved',
function($str)
{
$reserved = ['aaaa','bbbb','cccc','dddd'];
return (in_array(strtolower($str), $reserved)) ? TRUE : FALSE;
}),
),
'errors' => array(
'last_name_is_reserved' => 'The {field} field is reserved.',
),
);
$this->form_validation->set_rules($config);