0

I have a login function in CodeIgniter with this code:

public function login() {
    $this->redirect_if_logged($this->login_check());
    $this->data['active'] = 'login';

    $this->load->model('user_model');       
    $this->load->library('form_validation');

    $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');     
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback__validate_login');

    if (!$this->form_validation->run()) {
        $this->load->template('login', $this->data);
    } else {
        redirect('/','refresh');
    }       

}

And a validation function:

public function _validate_login($password) {
    $this->form_validation->set_message('_validate_login', 'Invalid username or password');
    return false;

}

The problem is that the custom function is never called, the validator always return true if all rules pass. The validator itself works, I checked it with other rules. It is just ignoring my custom function. What am I missing here?

5
  • did you debug inside _validate_login callback function, please try echo $password; die; Commented Mar 14, 2014 at 12:47
  • where is your _validate_login function? Commented Mar 14, 2014 at 12:51
  • I tried writing different things in that function, it always runs the redirect() line like it is not even checking that function. As in this example - I have set it to always return false, so it should not pass. Commented Mar 14, 2014 at 12:52
  • Both these functions are in the same controller. Commented Mar 14, 2014 at 12:53
  • i think $this->redirect_if_logged($this->login_check()); is redirect page Commented Mar 14, 2014 at 12:57

1 Answer 1

1

create MY_Form_validation extends CI_Form_validation in your library

 class MY_Form_validation extends CI_Form_validation
{

    public function _validate_login($password) {
  $this->form_validation->set_message('_validate_login', 'Invalid username or password');
  return false;

}

in your controller remove callback

$this->form_validation->set_rules('password', 'Password',  'trim|required|xss_clean|_validate_login')
Sign up to request clarification or add additional context in comments.

1 Comment

This works, although I needed to add this $CI =& get_instance(); and replace this with CI in the validator class. Thanks

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.