1
votes

I need to set form validation rules in codeigniter from an AJAX call The call generates a $_POST array where i nest data from different forms (1:N) and then I set validation rules in this way:

$this->form_validation->set_rules($field, $label , $rules);

and the $_POST array will be similar at this:

field1: value1
field2: value2
field3: value3
field4: value4
field5: value5
field6: value6      
id: 86
operation: "add"

clearly, some forms may have colliding names, and this solution is not reliable.

For example

field1:value1
field1:value2

names are given dinamically, so i can't afford to change them.

I choose to nest values in $_POST array:

form: 
    form1: 
           field1: value1
           field2: value2
    form2: 
           field1: value3
           field2: value4
    form3: 
           field1: value5
           field2: value6
id: 86
operation: "add"

but now form_validation is broken.

    $this->form_validation->set_rules('form[form1['.fieldN .']', $label   , $rules);

does not work as expected: i cannot validate. Looking into Form_validation.php library, set_rules first paramer is a string, and its value can be an array, but i can't go deeper with nesting e.g. array of array. There is a way to do this?Any hints?

2

2 Answers

2
votes

You can pass array as a field name like this

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

refer a details documentation for using array in Codeigniter's validation class.

2
votes

You can look into this link for reference how to set rules using a single array for all the fields.

You can set rules for different forms in same array in a nested array like this.

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


    );

Now all your rules for different forms in same array $config.