Search code examples
phpcodeignitervalidationmerge

Merge CodeIgniter form validation rules


I've just started a little page using CodeIgniter and wanted to run CodeIgniter's form validation magic tricks. For this, I've set some rules via config/form_validation.php:

$config = array(
    array(
        'field'   => 'name',
        'label'   => 'Name',
        'rules'   => 'trim|required|max_length[64]'
    )
);

But in addition to that, I wanted to set some specific rules inside the controller itself.

$this->form_validation->set_rules('name', 'Name', ' is_unique[table.name]');

My problem - the specific set_rules() seems to have reset all previously defined rules.

Is there a way to merge both set of rules? Or did I miss a method for that?


Solution

  • It is better to defined named array in the config file for each controller and use it as mentioned in the Codeginiter user guide.

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

    Call it like $this->form_validation->run('signup') with the name of the array.