简体   繁体   中英

Codeigniter form validate for optional fileds

I have form fields email and phone.Out of these any one need to be filled.

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

How can I do this?

You need to call a callbabk function

$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

In your callback you have to check empty fields for email and phone.

 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;
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM