Search code examples
phpcodeigniter

Codeigniter form validation callback not working


I'm trying to extend the validation class with my own function but it seems to skip the check.

My code

public function login_validation(){
 $this->load->library('form_validation');
 $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean|callback_validate_credentails');
 $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
    if($this->form_validation->run()){
        #redirect('home');
        //Just to check 
        echo "login success";
    }
    else{
        $this->load->view('view_login');
    }
}

My validate credentails function goes like this;

public function validate_credentials(){
   //loads model_user
   $this->load->model('model_users');
   //checks if login_validate function in model_users return true
    if($this->model_users->login_validate()){
        return true;
    }
    else{
        //set validation message
        $this->form_validation->set_message('validate_credentails','Incorrect Username/Password');
        return false;
    }

}

My login_validate function in model_users if needed;

public function login_validate(){
    $this->db->where('email', $this->input->post('email'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get('users');
    if($query->num_rows() == 1){
        return true;
    }
    else {
        return false; 
        }

}

This is the first time i've had this problem, am i doing something wrong? I've spent too much time on this :(


Solution

  • I just found the solution. I needed to change the name of the form_validation class as Nish suggested and add this function.

    class MY_Form_validation extends CI_Form_validation {
        function run($module = '', $group = ''){
            is_object($module) AND $this->CI = &$module;
            return parent::run($group);
        }
    }