Saff
Saff

Reputation: 563

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 :(

Upvotes: 2

Views: 4664

Answers (5)

Sayem
Sayem

Reputation: 104

For Codeigniter 3 form validation callback Just add public $CI in MY_Form_validation

path: application/libraries/MY_Form_validation

class MY_Form_validation extends CI_Form_validation
{
    public $CI;
}

and then on your controllers constructor:

$this->load->library('form_validation');
$this->form_validation->CI =& $this;

It's a HMVC related callback problem. By this solution the current controller as the $CI variable to the form_validation library before assigning. This will allow your callback methods to function properly.

Upvotes: 0

Waqar Haider
Waqar Haider

Reputation: 971

If you move the Callback_ function to the same controller it will work fine.

Upvotes: 0

Saff
Saff

Reputation: 563

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);
    }
} 

Upvotes: 3

Maxime Morin
Maxime Morin

Reputation: 2008

Your callback is callback_validate_credentails (tails) and your function name is validate_credentials (tials). Typo maybe?

Upvotes: 2

Svetoslav
Svetoslav

Reputation: 4686

I think the best advice is your LOGIN_validation check to be at your MODEL not at the form validation class... Its different THING !

Upvotes: 1

Related Questions