Callback Function Error ( Unable to access an erro

2019-09-04 13:45发布

问题:

I am new to Codeigniter. I have error when i try to check the email is existed or not. I saw lot of post on Stackoverflow and other website. I can't get any result.

When i try with below coding i got below errors

Unable to access an error message corresponding to your field name Email.(email_check)

Please check my code.

Controller

<?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;
        }       
    }
}

MODEL

<?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;
        }

    }   
}

?>

回答1:

I believe you are missing your callback function email_check, and there the set_message should correspond to the function and not the field itself.



回答2:

You will have to add the 2 callback functions that you are using :

<?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;
        }       
    }
}