【发布时间】:2011-07-24 10:29:16
【问题描述】:
有人可以帮我在 CodeIgniter 中进行条件字段验证吗?
尝试收集一些客户数据,如果用户在邮件单选按钮中选择“是”,则某些字段(例如地址、城市、邮政编码等)将成为必填项。
我在 config/form_Validation.php 中有 CodeIgniter 表单验证代码,如下所示:
$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
如果用户在邮件选项中选择“是”,我不确定如何将地址、邮政编码和其他字段设为“必填”。
如果有人可以帮助我,那就太好了。
非常感谢
问候, 附言
【问题讨论】:
标签: codeigniter callback