[eluser]FinalFrag[/eluser]
I'm just starting out with CodeIgniter and I ran up to a problem I cannot seem to fix. So I decided to pay another visit to the forums
I have a simple form with 2 fields: username and password. I don't want to use the validation rules that come with CodeIgniter because I want to have more control over the error message the user gets. I currently have this code:
Code:
function login() {
$this->load->library('validation');
$this->validation->set_error_delimiters('Functions::showMessage(\'error\', \'', '\');');
$rules['username'] = 'trim';
$rules['password'] = 'trim|callback__check_input';
$this->validation->set_rules($rules);
$fields['username'] = 'username';
$fields['password'] = 'password';
$this->validation->set_fields($fields);
if ($this->validation->run()) {
// everything is ok
redirect('home');
}
else {
$this->load->view('admin/login');
}
}
function _check_input() {
$username = trim($this->input->post('username'));
$password = trim($this->input->post('password'));
if (!empty($username) && !empty($password)) {
$this->load->model('users');
$result = $this->users->GetUserByUsername($username);
if ($result->num_rows() > 0) {
foreach ($result->result() as $row) {
if ($row->password == md5($password)) {
// the password is correct
$this->session->set_userdata('userId', $row->userId);
$this->session->set_userdata('username', $row->username);
$this->session->set_flashdata('info', 'Logged in as ' . $row->username . '.');
return true;
}
else {
$this->validation->set_message('_check_input', 'Incorrect username and/or password.');
}
}
}
else {
$this->validation->set_message('_check_input', 'Incorrect username and/or password.');
}
}
else {
$this->validation->set_message('_check_input', 'All fields are required.');
return false;
}
return false;
}
Can anyone tell me why _check_input is never called? Instead I just get redirected to 'home' if I leave all fields empty...
Thanks is advance...