忽略自定义验证函数的代码信号器 [英] codeigniter ignoring custom validation function

查看:143
本文介绍了忽略自定义验证函数的代码信号器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CodeIgniter中使用以下代码的登录功能:

I have a login function in CodeIgniter with this code:

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');
    }       

}

p>

And a validation function:

public function _validate_login($password) {
    $this->form_validation->set_message('_validate_login', 'Invalid username or password');
    return false;

}

问题是自定义函数从未被调用,如果所有规则通过,验证器总是返回true。验证器本身工作,我检查它与其他规则。它只是忽略我的自定义函数。

The problem is that the custom function is never called, the validator always return true if all rules pass. The validator itself works, I checked it with other rules. It is just ignoring my custom function. What am I missing here?

推荐答案

create MY_Form_validation在您的库中扩展CI_Form_validation

create MY_Form_validation extends CI_Form_validation in your library

 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;

}

在您的控制器中remove callback

in your controller remove callback

$this->form_validation->set_rules('password', 'Password',  'trim|required|xss_clean|_validate_login')

这篇关于忽略自定义验证函数的代码信号器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆