Validation Codeigniter表单验证$\u后嵌套字段

Validation Codeigniter表单验证$\u后嵌套字段,validation,codeigniter,Validation,Codeigniter,我需要通过AJAX调用在codeigniter中设置表单验证规则 该调用生成一个$\u POST数组,我在其中嵌套来自不同表单(1:N)的数据,然后以以下方式设置验证规则: $this->form_validation->set_rules($field, $label , $rules); 美元POST数组在以下情况下类似: field1: value1 field2: value2 field3: value3 field4: value4 field5: value5 fie

我需要通过AJAX调用在codeigniter中设置表单验证规则 该调用生成一个$\u POST数组,我在其中嵌套来自不同表单(1:N)的数据,然后以以下方式设置验证规则:

$this->form_validation->set_rules($field, $label , $rules);
美元POST数组在以下情况下类似:

field1: value1
field2: value2
field3: value3
field4: value4
field5: value5
field6: value6      
id: 86
operation: "add"
显然,某些表单的名称可能相互冲突,而这种解决方案并不可靠

比如说

field1:value1
field1:value2
名字都是亲笔给的,所以我没钱改

我选择在$\u POST数组中嵌套值:

form: 
    form1: 
           field1: value1
           field2: value2
    form2: 
           field1: value3
           field2: value4
    form3: 
           field1: value5
           field2: value6
id: 86
operation: "add"
但是现在表单验证被破坏了

    $this->form_validation->set_rules('form[form1['.fieldN .']', $label   , $rules);
未按预期工作:我无法验证。查看Form_validation.php库,set_rules first parameter是一个字符串,它的值可以是一个数组,但我无法深入了解嵌套,例如数组的数组。
有办法做到这一点吗?有什么提示吗?

您可以查看此链接,了解如何使用单个数组为所有字段设置规则

可以像这样在嵌套数组中为同一数组中的不同窗体设置规则

$config['login_form'] = array (
                        array 
                        (
                            'key' => 'email', 
                            'value' => 'Email',
                            'rule' => 'trim|required|valid_email|xss_clean'
                        ),
                        array
                        (
                            'key' => 'passwd', 
                            'value' => 'Password',
                            'rule' => 'trim|required|alpha_numeric|xss_clean'
                        )
                      );
 $config['login_form_error_code_1'] = 'The email or password you entered is incorrect';                       

 $config['add_user_form'] = array(
                            array(
                                    'key' => 'user_email',
                                    'value' => ' User Email',
                                    'rule' => 'trim|required|valid_email|callback_duplicate_user_email_check|xss_clean'
                                    ),
                            array(
                                    'key' => 'user_name',
                                    'value' => 'User Name',
                                    'rule' => 'trim|required|xss_clean'
                                     ),
                            array(
                                    'key' => 'user_phone',
                                    'value'=> 'Mobile',
                                    'rule' => 'trim|required|integer|min_length[10]|max_length[10]|xss_clean'
                                    ),
                            array
                            (
                                    'key' => 'user_password',
                                    'value' => 'Password',
                                    'rule' => 'trim|required|min_length[8]|alpha_numeric|xss_clean'
                            )


    );

现在,在同一数组$config中设置不同表单的所有规则。

您可以查看此链接,了解如何使用单个数组为所有字段设置规则

可以像这样在嵌套数组中为同一数组中的不同窗体设置规则

$config['login_form'] = array (
                        array 
                        (
                            'key' => 'email', 
                            'value' => 'Email',
                            'rule' => 'trim|required|valid_email|xss_clean'
                        ),
                        array
                        (
                            'key' => 'passwd', 
                            'value' => 'Password',
                            'rule' => 'trim|required|alpha_numeric|xss_clean'
                        )
                      );
 $config['login_form_error_code_1'] = 'The email or password you entered is incorrect';                       

 $config['add_user_form'] = array(
                            array(
                                    'key' => 'user_email',
                                    'value' => ' User Email',
                                    'rule' => 'trim|required|valid_email|callback_duplicate_user_email_check|xss_clean'
                                    ),
                            array(
                                    'key' => 'user_name',
                                    'value' => 'User Name',
                                    'rule' => 'trim|required|xss_clean'
                                     ),
                            array(
                                    'key' => 'user_phone',
                                    'value'=> 'Mobile',
                                    'rule' => 'trim|required|integer|min_length[10]|max_length[10]|xss_clean'
                                    ),
                            array
                            (
                                    'key' => 'user_password',
                                    'value' => 'Password',
                                    'rule' => 'trim|required|min_length[8]|alpha_numeric|xss_clean'
                            )


    );

现在,在同一个数组$config中为不同表单设置所有规则。

您可以将数组作为字段名传递,如下所示

$this->form_validation->set_rules('options[]', 'Options', 'required');

有关在Codeigniter的验证类中使用数组的信息,请参阅。

您可以将数组作为字段名传递,如下所示

$this->form_validation->set_rules('options[]', 'Options', 'required');

有关在Codeigniter的验证类中使用阵列的信息,请参阅a。

已经看到。据我所知,我可以将相同的规则集分配给固定的字段集(例如复选框)。我有动态字段名和动态规则,我的问题是将$\u POST[form[formname[fieldname]]绑定到正确的set\u规则(fieldname,label,rules)是否要将不同表单的所有规则放在一个数组中$this->form\u validation->set\u rules('form[form\u name],'try','required');工作正常,但我需要添加另一个嵌套级别,但它不再工作。抱歉,这是一个嵌套级别问题,而不是填充问题;)我刚从三个嵌套级别恢复到两个嵌套级别,它可以正常工作。form_验证库似乎最多只能接受2。已看到。据我所知,我可以将相同的规则集分配给固定的字段集(例如复选框)。我有动态字段名和动态规则,我的问题是将$\u POST[form[formname[fieldname]]绑定到正确的set\u规则(fieldname,label,rules)是否要将不同表单的所有规则放在一个数组中$this->form\u validation->set\u rules('form[form\u name],'try','required');工作正常,但我需要添加另一个嵌套级别,但它不再工作。抱歉,这是一个嵌套级别问题,而不是填充问题;)我刚从三个嵌套级别恢复到两个嵌套级别,它可以正常工作。表单验证库似乎最多只能接受2。$this->form\u validation->set\u rules('form[form\u name],'try','required');工作,但我需要添加另一个嵌套级别,它不再工作了。是的。我恢复到2级,运行正常。$this->form\u validation->set\u rules('form[form\u name],'try','required');工作,但我需要添加另一个嵌套级别,它不再工作了。是的。我恢复到2级,运行良好。