0

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

    }   
}

?>
Kalaivanan
  • 459
  • 2
  • 8
  • 17

2 Answers2

0

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.

  • Yes Right Sir. But when i change this, i got error on line 7 at model_users.php – Kalaivanan Sep 23 '15 at 18:31
  • Because you should pass string from input field to callback function. I.e. `public function email_check($str) {/* some code here */}`. Check [docs](http://codeigniter.com/userguide3/libraries/form_validation.html#callbacks-your-own-validation-methods) again. – Tpojka Sep 23 '15 at 18:59
0

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;
        }       
    }
}
Mohan
  • 4,677
  • 7
  • 42
  • 65