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

}
?>

我是codeigniter的新手。上面的代码不显示密码应该匹配错误信息。回调有问题还是我错过了什么。

4

3 回答 3

0

callback__matcherror作为函数的第四个参数传递。它set_rules应该是第三个参数。使用这种方式

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

笔记

如果您的密码字段匹配,您将收到此错误消息。因为您在那里应用了 3 条规则。当第 2 条规则成功时,将应用第 3 条规则(call_back_function)。当密码匹配时,您的第二条规则将有效。

于 2015-04-29T09:18:22.307 回答
0
matches[password]

会自动检查密码。你不需要使用回调函数callback__matcherror

于 2015-04-29T10:18:38.973 回答
0

看看这里。您无需进行回调。

于 2015-04-29T10:39:11.357 回答