4
votes

I'm trying to create a set of Form validation rules in my Codeigniter project, so that when the validation of the first set fails, the second validation set should not run.

I found this in the CI manual:

$config = array(
             'signup' => array(
                                array(
                                        'field' => 'username',
                                        'label' => 'Username',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'password',
                                        'label' => 'Password',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'passconf',
                                        'label' => 'PasswordConfirmation',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'email',
                                        'label' => 'Email',
                                        'rules' => 'required'
                                     )
                                ),
             'email' => array(
                                array(
                                        'field' => 'emailaddress',
                                        'label' => 'EmailAddress',
                                        'rules' => 'required|valid_email'
                                     ),
                                array(
                                        'field' => 'name',
                                        'label' => 'Name',
                                        'rules' => 'required|alpha'
                                     ),
                                array(
                                        'field' => 'title',
                                        'label' => 'Title',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'message',
                                        'label' => 'MessageBody',
                                        'rules' => 'required'
                                     )
                                )                          
           );
$this->form_validation->set_rules($config);

I know that I can now run the validation of each set separately ($this->form_validation->run('signup') and $this->form_validation->run('email') in this case).

The problem is, that when I use the $config array, errors do not get added to the Form validation class (and thus does not show up) while the form post failed. It did add and show errors when I did not use the set of rules, but just the $this->form_validation->set_rules() method.

What did I do wrong that no error messages are added when entering invalid form data while using a set of rules?

4

4 Answers

1
votes

The $config array needs to be in a file called form_validation.php in the application/config directory. It is then loaded automatically when CI is loaded, and passed to the Form validation object when it is created.

The first time the run() method of the FV object is called, if no rules have been set in the FV object, it looks up the config rules it was given on creation, and uses the group indexed by the name passed as argument to run(). All later calls to run() in the same invocation of the CI application, even with different group names, will bypass this check since the rules will now have been set - i.e., you only get to set the rule group once in an invocation.

So you won't be able to create two groups of rules and then call one after the other. You can either call one OR the other.

It might be better to cascade your rules using set_rule() - i.e., add some rules using set_rule(), then validate against them. if they pass, add some more rules and retry validation. You effectively repeat the old rules, knowing they will pass, but that means any failures will be the result of the new rules.

0
votes

Try array_merge in the form_validation array.

Here if you want two array to combint and gat join validation error. You can use this

$config["form"] = array_merge($config['signup'], $config['email']);

Hope this help.

0
votes

If someone is facing the same problem try this:

if ($this->form_validation->run('signup') === FALSE) { /* return your errors */ }

$this->form_validation->reset_validation();
$this->form_validation->set_data($data);
if ($this->form_validation->run('email') === FALSE) { /* return your errors */ }

// Do your stuff 

You need to reset after each validation to change the rules. You can also use:

$this->form_validation->set_rules($validation_rules);

Note: Set data first and then set rules, it doesn't work the other way around!

-1
votes

hey alwin u need to run the form_validation rules before submitting the form ....

$config = array(
             'signup' => array(
                                array(
                                        'field' => 'username',
                                        'label' => 'Username',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'password',
                                        'label' => 'Password',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'passconf',
                                        'label' => 'PasswordConfirmation',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'email',
                                        'label' => 'Email',
                                        'rules' => 'required'
                                     )
                                ),
             'email' => array(
                                array(
                                        'field' => 'emailaddress',
                                        'label' => 'EmailAddress',
                                        'rules' => 'required|valid_email'
                                     ),
                                array(
                                        'field' => 'name',
                                        'label' => 'Name',
                                        'rules' => 'required|alpha'
                                     ),
                                array(
                                        'field' => 'title',
                                        'label' => 'Title',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'message',
                                        'label' => 'MessageBody',
                                        'rules' => 'required'
                                     )
                                )                          
           );


 $this->form_validation->set_rules($config);
    ///u have to chek form validation getting validate or not
   //enter code here


 if ($this->form_validation->run() === FALSE) {
                $this->load->view('your_view');
            } else {$this->ur_controller->method_name();
                $this->load->view('whatever ur view');

            }
        }