我有两个数字字段来收集用户的数据。需要使用 codeigniter 表单验证类来验证它。
状况:
目前我使用
$this->form_validation->set_rules('first_field', 'First Field', 'trim|required|is_natural');
$this->form_validation->set_rules('second_field', 'Second Field', 'trim|required|is_natural_no_zero');
但是,如何验证上述第三和第四个条件?
提前致谢。
最佳答案
谢谢 dm03514。我通过下面的回调函数让它工作。
$this->form_validation->set_rules('first_field', 'First Field', 'trim|required|is_natural');
$this->form_validation->set_rules('second_field', 'Second Field', 'trim|required|is_natural_no_zero|callback_check_equal_less['.$this->input->post('first_field').']');
和回调函数是:
function check_equal_less($second_field,$first_field)
{
if ($second_field <= $first_field)
{
$this->form_validation->set_message('check_equal_less', 'The First &/or Second fields have errors.');
return false;
}
return true;
}
现在一切似乎都运行良好:)
关于代码点火器 : Validating two fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15367265/