Php 如何将_规则设置为插入两个字段之一?

Php 如何将_规则设置为插入两个字段之一?,php,codeigniter,Php,Codeigniter,如果我想从两个输入字段中选择一个,则必须正确填写。然后只有表单被提交,否则它将显示错误我们如何使用codeigniter中classform\u validation中的set\u rule函数进行验证。假设(为了本例的目的)您想要验证电子邮件地址或电话号码,然后以最简单的形式进行验证 // If no email address, phone is required if ( ! $this->input->post('email')) { $this->form_va

如果我想从两个输入字段中选择一个,则必须正确填写。然后只有表单被提交,否则它将显示错误我们如何使用codeigniter中class
form\u validation
中的
set\u rule
函数进行验证。

假设(为了本例的目的)您想要验证电子邮件地址或电话号码,然后以最简单的形式进行验证

// If no email address, phone is required
if ( ! $this->input->post('email')) {
   $this->form_validation->set_rules('phone', 'Phone Number', 'trim|numeric|min_length[11]|required');
} else {
   $this->form_validation->set_rules('phone', 'Phone Number', 'trim|numeric|min_length[11]');
}

// If no phone number, email is required
if ( ! $this->input->post('phone')) {
   $this->form_validation->set_rules('email', 'Email Address', 'trim|valid_email|required');
} else {
   $this->form_validation->set_rules('email', 'Email Address', 'trim|valid_email');
}
…你也许可以把它整理一下,但这就是我认为你想要得到的东西的大概意思

希望能有帮助