【问题标题】:How to validate multidimensional arrays with Codeigniter and Jquery如何使用 Codeigniter 和 Jquery 验证多维数组
【发布时间】:2012-09-09 22:46:53
【问题描述】:

您好,我需要验证这样的多维表单

<input type="text" class="input-xlarge span5 req" id="contact_first_name" name="hotel[<?=$id?>][contact_first_name]" value="<?= set_value('hotel[contact_first_name]') ?>">
<input type="text" class="input-xlarge span5 req" id="contact_last_name" name="hotel[<?=$id?>][contact_last_name]" value="<?= set_value('hotel[contact_last_name]') ?>">

我不知道最终数组的维度,因为输入是通过 jquery 动态添加的。

我在服务器端使用 Codeigniter Form_Validation,在客户端使用 JQuery Validator。

这是我的 form_validation 规则

$config['add_hotel'] = array(
array(
    'field' => 'hotel[][hotel_name]', 
    'label' => 'Hotel Name', 
    'rules' => 'required'
    ),    
array(
    'field' => 'hotel[][contact_first_name]', 
    'label' => 'First Name', 
    'rules' => 'trim|required'
    ),
array(
    'field' => 'hotel[][contact_last_name]', 
    'label' => 'Last Name', 
    'rules' => 'trim|required'
    ),

这就是我通过 jquery 验证器的方式

$("#add_hotel").validate({
rules: {
    "hotel[][hotel_name]": "required"

  /*  errorElement: "div",
    wrapper: "div"*/
},
messages: {
   "hotel[][hotel_name]": "Please enter the Association Name"
},
submitHandler: function(form) {
    form.submit();
}

不知道如何使用自己的 id 验证每个 Hotel[] 输入,或者可能有另一种方法可以更简单地定义输入。

【问题讨论】:

    标签: jquery arrays codeigniter validation multidimensional-array


    【解决方案1】:

    张贴数组

    $hotel = $this->input->post('hotel');
    if(!empty($hotel))
    {
        // Loop through hotels and add the validation
        foreach($hotel as $id => $data)
        {
            $this->form_validation->set_rules('hotel[' . $id . '][contact_first_name]', 'First name', 'required|trim');
            $this->form_validation->set_rules('hotel[' . $id . '][contact_last_name]', 'Last name', 'required|trim');
        }
    }
    

    始终适用的默认规则

    $this->form_validation->set_rules('username', 'Username', 'required');
    
    if ($this->form_validation->run() == FALSE)
    {
        // Errors
    }
    else
    {
        // Success
    }
    

    【讨论】:

    • 非常感谢,这对 CI Form_validation 有用。现在我必须通过 JQuery Validator 插件对其进行验证,但不知道在验证中创建 var 索引的方法
    • 是否可以避免循环并在config/form_validation.php中设置验证规则?
    【解决方案2】:

    Codeigniter 有一种非常严格的方法来处理命名为数组的输入的验证规则,它只会验证字段名称是否完全相同,所以规则

    array(
        'field' => 'hotel[][hotel_name]', 
        'label' => 'Hotel Name', 
        'rules' => 'required'
        ),    
    

    仅当字段实际命名为 hotel[][hotel_name] 时才有效。
    由于这不是字段的名称(实际名称类似于 hotel[1][hotel_name]),因此 Codeigniter 不会对其进行验证。

    您可以动态生成配置数组,但我认为您最好分别为这些字段编写自己的验证规则。

    【讨论】:

      【解决方案3】:
         is not working i am trying to like this
           in api data 
           {
            "fullname":"4545",
          "family_detail": [
             {
              "relation_id":""
            },
            {
              "relation_id":"11"
             }]
          }
      
         in controller 
         $_POST = json_decode(file_get_contents('php://input'),true);
      
         if ($this->form_validation->run('signup') == FALSE)
          {
           //error
          }else{
            // success
          } 
           in my config file
           $config = array(
         'signup' => array(   
          array(
           'field' => 'fullname',
           'label' => 'Full Name',
          'rules' => 'required'
            ),
           array(
          'field' => 'family_detail[][relation_id]',
          'label' => 'relation_id',
          'rules' => 'required'
           ),
           )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-25
        • 2017-05-28
        • 2011-09-01
        • 1970-01-01
        • 2014-06-06
        • 1970-01-01
        • 2014-09-13
        • 2016-07-14
        相关资源
        最近更新 更多