I've written a simple callback function which isn't working. My other callbacks (which are in the same library file) work fine so I guess the problem has to do with my code.
The parameter passed in the callback function takes the form of a chunk of PHP which is eval()'ed to form part of an 'if()' statement in the function itself.
Here's what's in the controller:
$this->form_validation->set_rules('rating', 'Rating','required');
$condition = $this->input->post('rating') . " != 'Excellent'";
$this->form_validation->set_rules('details', 'Details', 'required_conditional[' . htmlentities($condition) .']');
And here's the callback function itself:
function required_conditional($str, $condition)
{
if (eval(html_entity_decode($condition))) {
if ($str == '') {
$this->set_message('required_conditional', 'The %s field is required');
return FALSE;
}
else {
return TRUE;
}
}
}
Any ideas why it's not working anyone?
Thanks, Matt
It's because eval
evaluates statements, not expressions. This will give you a parse error:
$test = "1 > 0";
if (eval($test)) { echo "echo!"; }
And this will work as you expect it to:
$test = "return 1 > 0;";
if (eval($test)) { echo "echo!"; }
shouldn't you use "callback_<function name>
" ?
Yep the correct syntax to call form validation callbacks it to use "callback_"