回调函数错误(无法访问与您的字段名称对应的错误消息)


Callback Function Error ( Unable to access an error message corresponding to your field name )

我是Codeigniter的新手。我有错误,当我试图检查电子邮件是否存在。我在Stackoverflow和其他网站上看到了很多帖子。我找不到任何结果。

当我尝试使用下面的编码时,我得到了以下错误

无法访问与您的字段名称对应的错误消息电子邮件。(email_check)

请检查我的代码。

控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
    public function index()
    {
        $this->login();
    }
    public function login()
    {
        $this->load->view('login');
    }
    public function login_validation()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules("email","Email","required|trim|callback_email_check");
        $this->form_validation->set_rules("password","Password","required|md5|trim|callback_password_check");
        if($this->form_validation->run())
        {
            redirect('main/members');
        }
        else
        {
            $this->load->view('login');
        }
    }
    public function members()
    {
        $this->load->model('model_users');
        if($this->model_users->can_log_in())
        {
            return true;
        }
        else
        {
            $this->form_validation->set_message('email_check', 'Incorrect Username/Password');
            return false;
        }       
    }
}

<?php
class Model_users extends CI_Model
{
    public function can_log_in()
    {       
        $this->db->where->('email',$this->input->post('email'));
        $this->db->where->('password',md5($this->input->post('password')));
        $query = $this->db->get('users');
        if($query->num_rows == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }   
}
?>

我相信您错过了回调函数email_check,并且set_message应该对应于函数而不是字段本身。

您必须添加您正在使用的两个回调函数:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
    public function index()
    {
        $this->login();
    }
    public function login()
    {
        $this->load->view('login');
    }
    public function login_validation()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules("email","Email","required|trim|callback_email_check");
        $this->form_validation->set_rules("password","Password","required|md5|trim|callback_password_check");
        if($this->form_validation->run())
        {
            redirect('main/members');
        }
        else
        {
            $this->load->view('login');
        }
    }
    public function email_check(){
        //perform your validation here
        if({your_validation_result}){
           return true;
        }else{
           $this->form_validation->set_message('email_check', 'Incorrect Username/Password');
        return false;
        }
    }
    public function password_check(){
            //perform your validation here
            if({your_validation_result}){
               return true;
            }else{
               $this->form_validation->set_message('password_check', 'Incorrect Username/Password');
            return false;
            }
     }


    public function members()
    {
        $this->load->model('model_users');
        if($this->model_users->can_log_in())
        {
            return true;
        }
        else
        {
            $this->form_validation->set_message('email_check', 'Incorrect Username/Password');
            return false;
        }       
    }
}