如何验证多个字段并在codeigniter中显示一个错误?

时间:2016-07-25 12:25:19

标签: php codeigniter

我有两个场地。

   $this->form_validation->set_rules($time_am,'AM Time','required');
   $this->form_validation->set_rules($time_pm,'PM Time','required');

我不想单独验证。如果任何一个或两个都是空的,则必须验证“需要AM和PM时间字段”。

在DB中我这样存储。

        $time_am    = $this->input->post('time_am');
        $time_pm    = $this->input->post('time_pm');
        if(empty($time_am) || empty($time_pm))
        {
            $time  = "NULL";    
        }
        else
        {
            $time   = $time_am." - ".$time_pm;
        }

2 个答案:

答案 0 :(得分:0)

你只需要这样做

if( ! empty($time_am) ){
    $this->form_validation->set_rules($time_am,'AM Time','trim|required|callback_valid_time');
}
else{
    $this->form_validation->set_rules($time_pm,'PM Time','trim|required|callback_valid_time');
}

if ($this->form_validation->run() === FALSE){
    ...
}
else{
    ...
}

答案 1 :(得分:0)

最后我找到了我的问题的答案。

arr.astype(int)