Codeigniter表单验证可选文件

时间:2015-09-30 05:46:07

标签: php codeigniter

我有表单字段电子邮件和电话。其中任何一个都需要填写。

$this->form_validation->set_rules('phone[]', 'Phone', 'trim|required|xss_clean');
$this->form_validation->set_rules('email[]', 'Email', 'trim|required|xss_clean');

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要调用callbabk函数

$this->form_validation->set_rules('phone[]', 'Phone', 'trim|xss_clean');
$this->form_validation->set_rules('email[]', 'Email', 'trim|xss_clean|callback_check_validation');// call a calback

在您的回调中,您必须检查电子邮件和电话的空白字段。

 function check_validation() {
       $phone=$this->input->post('phone');
       $email=$this->input->post('email');
       if(empty($phone) && empty($email)){// check empty of both filed
        $this->form_validation->set_message("check_validation", 'Atleast phone or email required');
        return FALSE;
       }else {
        return TRUE;
      }
    }