我在CodeIgniter中有一个登录函数,代码如下:
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');
}
}
验证功能:
public function _validate_login($password) {
$this->form_validation->set_message('_validate_login', 'Invalid username or password');
return false;
}
问题是永远不会调用自定义函数,如果所有规则都通过,验证器总是返回true。验证器本身有效,我用其他规则检查了它。它只是忽略了我的自定义功能。我在这里缺少什么?
答案 0 :(得分:1)
创建MY_Form_validation扩展库中的CI_Form_validation
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;
}
控制器中的删除回调
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|_validate_login')