在codeigniter中设置自定义错误消息


setting custom error messages in codeigniter

我在使用codeigniter时遇到一个问题。请帮我解决

我的控制器

$result=$this->forgot_password_model->reset_pass($username);
if(!$result){
     $this->form_validation->set_message('valid_email','Enter a valid email');
}

我的模型

    function reset_pass($username){
            $query = $this->db->get_where('users', array('useremail' => $username));
            if($query -> num_rows() == 1)
            {
                $row = $query->row_array();
                return $row['useremail'];
            }
            else
            {
                return false;
            }
}

$this->form_validation->set_message('valid_email','Enter a valid email');

上面的行不起作用,我无法设置数据库验证错误消息。请帮我一下。

感谢

您需要在控制器上使用回调函数,然后加载模型进行回调http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks

注意:如果使用codeigniter hmvc运行($this),则需要一个MY_form_validation库。

<?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('username', 'Username', 'callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
        // if ($this->form_validation->run($this) == FALSE) only use $this if have hmvc
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
    public function username_check()
    {
        $this->load->model('name');
        // Examples: 
        $username = $this->input->post('username'); 
        $password = $this->input->post('password'); 
        $email = $this->input->post('email'); 
        if ($this->name->reset_pass($username, $password, $email) == false)
        {
            $this->form_validation->set_message('username_check', 'Username Or Email Or Password Incorrect');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}
?>

MY_Form_validation Library(如果使用代码点火器hmvc)

// Controller method $this->form_validation->run($this)
<?php
class MY_Form_validation extends CI_Form_validation {
    function run($module = '', $group = '') {
        (is_object($module)) AND $this->CI = &$module;
        return parent::run($group);
    }
} 

验证的第一组规则

//set rule for your field
$this->form_validation->set_rules('email', 'Email', 'callback_email_check');
public function email_check($username)
    {
        //call your model
        $result=$this->forgot_password_model->reset_pass($username);
        if(!$result){
            $this->form_validation->set_message('valid_email','Enter a valid email');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

在此处查看文档:链接