1
有人可以帮我在CodeIgniter中进行条件字段验证吗?CodeIgniter条件验证
试图收集一些客户数据,如果用户在邮件单选按钮中选择“是”,则某些字段(如地址,城市,邮政编码等)变为强制性。
我有笨形式验证代码在配置/ form_Validation.php如下:
$config = array ('customer/new_customer' => array
(
array ('field' => 'firstName', 'label' => 'First Name', 'rules' => 'required'),
array ('field' => 'lastName', 'label' => 'Last Name', 'rules' => 'required'),
array ('field' => 'mail', 'label' => 'Mail', 'rules' => 'required'),
array ('field' => 'address', 'label' => 'Address','rules' => ''),
//other fields here
)
);
我在控制器下面的代码添加/编辑顾客:
function new_customer()
{
$customer_id = $this->input->post('customer_id');
if ($this->form_validation->run() == FALSE)
{
if(($customer_id != "X") && ($customer_id != "")){
$data['add_or_edit'] = "add";
return $this->edit_customer($customer_id, 'add');
}else {
$data['title'] = "New Customer";
$data['add_or_edit'] = 'add';
$this->load->view('customer_form_view',$data);
}
}else{
$data['firstName'] = $this->input->post('firstName');
$data['lastName'] = $this->input->post('lastName');
if($this->input->post('mail') == "Yes")
{
$data['address'] = $this->input->post('address');
$data['city'] = $this->input->post('city');
//other fields
}
if(($customer_id == 'X') || ($customer_id == ''))
{
//add new customer
$customer_id = $this->customers_model->insertCustomer($data);
redirect("/customer/customerList");
}else{
//edit the customer matching the customerID
$this->customers_model->editCustomer($customer_id, $data);
redirect("/customer/customerlist");
}
}//end validation if
}//end function
我不知道如何使地址,邮政编码等领域的“必要”,如果用户选择“是”为在邮件选项中。
如果有人能帮助我,这将是一件好事。
非常感谢
问候, PS
感谢您的回复。我会尝试.. 所以我会有邮件选项验证规则和实际的回调函数在config/form_validation.php文件是啊? – Prats
可以在控制器中使用验证规则。回调函数应该在控制器中。检查文档。 –
@Prats,确切地说,您将上面的函数mail_check()放在您为邮件和其他字段设置验证规则的相同控制器中。 – toopay