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.

the smart life
  • 305
  • 8
  • 26
  • Please check this link :- https://stackoverflow.com/questions/19324420/callback-function-in- codeigniter-with-multiple-parameters-during-form-validation – Praveen Kumar Dec 28 '17 at 09:12

2 Answers2

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

Pradeep
  • 9,667
  • 13
  • 27
  • 34
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.

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26