1

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

$this->form_validation->set_rules('form[form_name]', 'try' , 'required'); works, but i need to add another nesting level, and it doesn't work anymore.
Yes. I reverted to 2 level and it runs fine.
2

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.

6 Comments

Already seen. As I understand it just say that i can assign same ruleset to a fixed fieldset (e.g. checkboxes). I've dynamic fields name with dynamic rules, my problem is to bind $_POST[form[formname[fieldname]]] to the correct set_rules(fieldname, label, rules)
do u want to put all the rules in one array for different forms
$this->form_validation->set_rules('form[form_name]', 'try' , 'required'); works, but i need to add another nesting level, and it doesn't work anymore.
sorry, was a nesting levels issue and not a populating one ;)
i just reverted to two nesting levels from three and it works. The form_validation library seems to accept at most 2.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.