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

时间:2015-02-18 07:54:32

标签: php 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');

以上行无效,我无法设置数据库验证错误消息。请帮帮我。

谢谢

2 个答案:

答案 0 :(得分:0)

您需要在控制器上使用回调函数,然后将模型加载到回调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库,如果使用codeigniter-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);
    }

} 

答案 1 :(得分:0)

首先设定验证规则

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

在此处查看文档:{​​{3}}