Say I have a login form in CodeIgniter — I can set validation rules for individual inputs, but is there a way to throw a model/controller level error and a message?
Specifically, if the below method does not return TRUE, I want my form to re-display with the message “The email address or password is incorrect”. Currently the controller just reloads the view and the set_value()s
public function authorize_user()
{
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', $this->input->post('password'));
$q = $this->db->get('users');
if($q->num_rows() == 1){
return true;
}
}
Perhaps I’m overthinking this and I should just attach that error message to the email input?
You can use a callback function to accomplish this. The steps are as follow:
1. Your
authorize_user()function must be in the controller you set the rules.2. You make a “callback” rule by adding a code similar to:
Note that I added a parameter for the callback function. These kind of functions automatically receive a parameter which is determined by the first argument of
set_rules(). In this case, the argument passed automatically to the callback function is the email. Aditionally, I pass the password as second parameter.3.Add the respective parameters to your function:
More info at: http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks
Hope it helps!