0

Hello i am trying to create a validation callback for my form to validate a field only if it is not empty. I created my validation rules into a config file named form_validation.php. Here is a part of its content :

$config=array(
  'Users_Controller/add'=>array(
        array(
            'field'=>'email',
            'label'=>'email',
            'rules'=>'callback_email'
        ),
   )
);

public function email($value){
   if( trim( strlen($value) )!==0 ){
      $this->form_validation->set_rules(
         'email',
         'email',
         'valid_email|is_unique[users.email]',
          array(
             'valid_email'=>'invalid email',
             'is_unique' => 'email already registered'
         )
      )
   }
}

In my controller,in users_Controller.php here is how i handle the form submission :

class Users_Controller  extends CI_Controller{
     public function add(){
         if($this->form_validation->run()){
              $this->User_model->save();
              $this-session->set_flashdata('success','okay');
              redirect('users/list');
         }
         else {
              $this->load->view('add')
         }
     }
}

Unfortunately, the validation does not take in account the callback validation method i added in the config file form_validation.php but if i put this callback in the controller, the validation process ignore all the others rules puted in form_validation.php

Any help ? Thanks in advance.

1

2 Answers 2

1

Hope this helps you

form_validation.php

/*put callback rule in form_validation.php*/

$config=array(
   'Users_Controller/add'=>array(
    array(
        'field'=>'email',
        'label'=>'email',
        'rules'=>'callback_check_email'
    ),
  )
);

put callback method in Users_controller.php

In Users_controller.php

function check_email($value)
{
    /*check your email rule here as u want */
    /* this is just example*/ 

    $email= $this->YourModel->getemailofUser();
    if ($value == $email) {
        return TRUE;
    }
    else 
    {
        $this->form_validation->set_message('check_email', 'Your email is not a valid registered email!');
        return FALSE;
    }

}

For more : https://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

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

2 Comments

thanks i works, but i wanted to keep the callback function in the config file. is there a way to dot that ?
i never implement that as no descriptions are given in documentation
0

In order to call a specific validation rules group, such as Users_Controller/add, you need to pass group name to the run() method.
Users_controller.php

    ...
    public function add(){
        if($this->form_validation->run('Users_Controller/add')){
        ...
    }
    ...

And for a validation callback to work properly, you have to place the callback method within the same controller of the method that is calling the callback.

1 Comment

Thanks. i finaly found the solution by adding moving my callback function to the controller.

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.