callback not working in codeigniter 3.0

1.6k Views Asked by At
<?php

class Form extends CI_Controller {

    public function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('confpassword', 'Password', 'required|matches[password]', 'callback__matcherror');



        //$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        //$this->form_validation->set_rules('email', 'Email', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('login');

        }
        else
        {

            $this->load->view('insert_dream');

        }

    }

public function _matcherror() {
    $this->form_validation->set_message('_matcherror', 'Passwords should match');
    return FALSE;
}

}
?>

i am a newbie to codeigniter. The above code doesnt display passwords should match error message. Is something wrong with the callback or Am i missing something.

3

There are 3 best solutions below

0
George On BEST ANSWER

Take a look here. You don't need to make a callback.

0
Shaiful Islam On

You are passing callback__matcherror as fourth parameter of set_rules function.It should be 3rd parameter. Use this way

$this->form_validation->set_rules('confpassword', 'Password', 'required|matches[password]|callback__matcherror');

Note

You will get this error message if your password fields match.Because you applying 3 rule there.3rd rule(call_back_function) will apply when 2nd rule is success. Your 2nd rule will valid when passwords matches.

0
Abhishek Sharma On
matches[password]

will automatically check for password. You need not to use callback function callback__matcherror