验证在登录表单 CodeIgniter 中输入的错误信息

Validate wrong information entered in login form CodeIgniter

我的问题似乎是将 $data['error'] 数组传递给视图,因为在输入错误数据时我没有收到任何错误:

我的表单发布到这个控制器索引函数:

public function index() {

    $data = array();
    $data['title'] = 'Member Login';

    $this->load->model('auth_model');

    $remember = $this->input->post('remember_me');

    $this->auth_model->process($remember);
    $this->template->content = View::factory('login', $data);

}

这里是模型处理函数:

public function process($remember = null){

    $this->load->library('form_validation');
    $this->form_validation->set_rules('email_address', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[8]');

    if($this->form_validation->run()){

        //form validated
        if($res = $this->verify_user($this->input->post('email_address'), $this->input->post('password'))){
            //user verified
        } else {
            $data['errors'] = 'Incorrect username and or Password';
        }
    }
}

在视图中,我使用 echo validation_errors();

访问表单错误

如果我提交包含错误数据的表单,我不会收到任何错误消息...为什么?

此致

更新控制器功能,如图所示。

 public function index() {

        $data = array();
        $data['title'] = 'Member Login';

        $this->load->model('auth_model');

        $remember = $this->input->post('remember_me');

        $data['errors'] = $this->auth_model->process($remember);
        $this->template->content = View::factory('login', $data);

    }

Return 错误如下所示。

public function process($remember = null){

    $this->load->library('form_validation');
    $this->form_validation->set_rules('email_address', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[8]');

    if($this->form_validation->run()){

        //form validated
        if($res = $this->verify_user($this->input->post('email_address'), $this->input->post('password'))){
            //user verified
        } else {
           return 'Incorrect username and or Password';
        }
    }
}