and*_*222 0 php validation codeigniter
我正在尝试对必须具有不同值的2字段进行验证.我只知道如何设置验证匹配值的规则.
$this->form_validation->set_rules('book1','Book1','required|matches[book2]');
$this->form_validation->set_rules('book2','Book2','required|matches[book1]');
如果我输入book1= novel,和book2= novel,上面的代码将返回TRUE.
如何验证每个字段的值彼此不匹配的2个字段?所以,如果我输入book1= novel和book2= comic,它将返回TRUE.
您应该使用callback_方法进行自定义验证,CI表单验证库不提供notMatch类型验证规则,请参阅下面的示例代码.
$this->form_validation->set_rules('book1','Book1','required');
$this->form_validation->set_rules('book2','Book2','required|callback__notMatch[book1]');
并在控制器类中放置方法
function _notMatch($book2Value, $book1FieldName){
   if($book2Value != $this->input->post($book1FieldName){
       $this->form_validation->set_message('_notMatch', 'book1 and book2 values are not matching');
       return false;
   }
   return true;
}