0

I'm using a form_validation in my CI application and while using a callback function in set_rules() I got an error:

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

My Controller code is:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends MX_Controller 
{
    // public function __construct()
    // {
    //  parent::__construct();
    // }

    // public function index()
    // {
    //  $this->load->view('auth/login_v');
    // }

    public function login()
    {
        $this->load->view('auth/login_v');
    }

    public function login_validation()
    {
        //$this->load->library('form_validation');

        $this->form_validation->set_rules('email', 'Email', 'required|trim|callback_validate_credentials');
        $this->form_validation->set_rules('password', 'Password', 'required|md5|trim');

        if($this->form_validation->run())
        {
            redirect('dashboard');
        } else {
            $this->load->view('auth/login_v');
        }

        //echo $this->input->post('email');
    }

    public function validate_credentials()
    {
        $this->load->model('Auth_model');

        if($this->Auth_model->can_log_in())
        {
            return true;
        } else {
            $this->form_validation->set_message('validate_credentials', 'Incorrect Email/Password');

            return false;
        }
    }
}

I don't know where am I mistaking. I had pass the parameters to tha callback function:

$this->form_validation->set_rules('email', 'Email', 'required|trim|callback_validate_credentials[email]');

and

public function validate_credentials($email)
    {
        $this->load->model('Auth_model');

but I got same error.

I have referred following sites:

In Codeigniter, how to pass a third parameter to a callback (form validation)?

Unable to access an error message corresponding to your field name

Callback function in Codeigniter with multiple parameters during form validation

CodeIgniter callback functions with parameters

https://codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

CodeIgniter - Unable to access an error message corresponding to your field name Password.(pword_check)

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

I'm using codeigniter 3.1.6 and HMVC for my application.

Any kind of help will always welcome. Thanks

Ganesh Aher
  • 1,118
  • 3
  • 21
  • 51
  • Load this this->load->library('form_validation') in the __construct area of controller https://www.codeigniter.com/user_guide/general/controllers.html#class-constructors –  Sep 27 '17 at 20:00
  • I've already loaded form_validation library in my autoload.php file. Steel I've to load it in my __construct() function ? – Ganesh Aher Sep 27 '17 at 20:02
  • Same error. No any effect after: public function __construct() { parent::__construct(); $this->load->library('form_validation'); } – Ganesh Aher Sep 27 '17 at 20:05
  • Also just a tip don't use md5 for passwords as very unsecure for passwords you should hash with this http://php.net/manual/en/function.password-hash.php and then verify with this http://php.net/manual/en/function.password-verify.php –  Sep 27 '17 at 21:28
  • in your calback you need to get a parameter like `public function validate_credentials( $str = '')` called with this `$this->form_validation->set_rules('email', 'Email', 'required|trim|callback_validate_credentials');` – elddenmedio Sep 27 '17 at 21:38
  • I suspect your beloved HMVC is causing your problems. See: https://github.com/krishnabhat81/Codeigniter-HMVC-form-validation-callback-not-working – Brian Gottier Sep 28 '17 at 00:55
  • @BrianGottier actually I already go for that, but when I'm extending the class in library, it give an error. First I'm extending class CI_Form_validation and then made a changes to MX_Form_validation. Both gives errors. Will you please give the sample code for library class. – Ganesh Aher Sep 28 '17 at 05:13
  • Inside set_rule callback, do not pass the parameter. i.e. $this->form_validation->set_rules('email', 'Email', 'required|trim|callback_validate_credentials'); Pass the argument in the function declaration. i.e. public function validate_credentials($email). Take look in CI documentation of callback. http://www.codeigniter.com/userguide3/libraries/form_validation.html#callbacks-your-own-validation-methods – Jay Gosai Sep 28 '17 at 07:06

0 Answers0