【问题标题】:codeigniter ignoring custom validation functioncodeigniter 忽略自定义验证功能
【发布时间】:2014-03-14 12:41:33
【问题描述】:

我在 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。验证器本身可以工作,我用其他规则检查了它。它只是忽略了我的自定义功能。我在这里错过了什么?

【问题讨论】:

  • 你在_validate_login回调函数里面调试了吗,请试试echo $password; die;
  • 你的 _validate_login 函数在哪里?
  • 我尝试在该函数中编写不同的东西,它总是运行 redirect() 行,就像它甚至没有检查该函数一样。如本例所示 - 我已将其设置为始终返回 false,因此不应通过。
  • 这两个函数在同一个控制器中。
  • 我认为$this->redirect_if_logged($this->login_check()); 是重定向页面

标签: php codeigniter validation


【解决方案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')

【讨论】:

  • 这可行,尽管我需要在验证器类中添加这个$CI =& get_instance(); 并用CI 替换this。谢谢
猜你喜欢
  • 2013-02-28
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多