CodeIgniter 条件验证

发布于 2024-11-25 18:24:38 字数 1983 浏览 1 评论 0原文

有人可以帮我在 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

如果用户在邮件选项中选择“是”,我不确定如何将地址、邮政编码和其他字段设置为“必填”。

如果有人能帮助我解决这个问题,那就太好了。

非常感谢

问候, 聚苯乙烯

Can someone please help me with this conditional field validation in CodeIgniter?

Trying to collect some customer data and if the user selects 'Yes' in the mail radio buttons, some of the fields such as (address, city, postcode etc) becomes mandatory.

I have the CodeIgniter form validation code in config/form_Validation.php as below:

$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
)

);

I have the code below in the controller to add/edit customer:

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

I am not sure how to make the address, postcode and other fields as 'required' if the user selects 'Yes' in the mail option.

It will be great if someone can help me with this.

Thanks a lot

Regards,
PS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2024-12-02 18:24:39

您可以使用回调函数作为您的邮件选项验证规则...类似的东西

$this->form_validation->set_rules('mail', 'Mail', 'callback_mail_check');

然后在回调函数中,您可以有类似的东西

function mail_check($str)
{
    if ($str == 'YES')
    {
        $this->form_validation->set_message('mail_check', 'You need to fill other fields.');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

You could use callback function, as your mail option validation rule... Something like

$this->form_validation->set_rules('mail', 'Mail', 'callback_mail_check');

Then in callback function, you could have something like

function mail_check($str)
{
    if ($str == 'YES')
    {
        $this->form_validation->set_message('mail_check', 'You need to fill other fields.');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文