如何使用Codeigniter和Jquery验证多维数组

use*_*153 7 arrays validation jquery codeigniter multidimensional-array

嗨,我需要验证这样的多维形式

<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]') ?>">
Run Code Online (Sandbox Code Playgroud)

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

我在服务器端使用Codeigniter Form_Validation,在客户端使用JQuery Validator通过JQuery.

这是我的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'
    ),
Run Code Online (Sandbox Code Playgroud)

这就是我通过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();
}
Run Code Online (Sandbox Code Playgroud)

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

Phi*_*ilo 9

发布数组

$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');
    }
}
Run Code Online (Sandbox Code Playgroud)

始终适用的默认规则

$this->form_validation->set_rules('username', 'Username', 'required');

if ($this->form_validation->run() == FALSE)
{
    // Errors
}
else
{
    // Success
}
Run Code Online (Sandbox Code Playgroud)