I have a login form with validation that works except for one use case.
If a user enters a valid email address and no password I want only the "The password is required" message to appear but right now both "The password is required" message and the "Incorrect Email/Password" message are appearing.
I understand why but don't know how to change this code so that the callback only runs if the other validation has been passed.
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
$this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div><br />');
if ($this->form_validation->run()) {
$data = array(
'email' => $this->input->post('email'),
'staff_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('clock/clock');
} else {
$this->load->view('login/login');
}
}
public function validate_credentials() {
$this->load->model('staff_model');
if ($this->staff_model->can_log_in()) {
return true;
} else {
$this->form_validation->set_message('validate_credentials', 'Incorrect Email/Password');
return false;
}
}