Search code examples
model-view-controllercodeigniter-2

Validate wrong information entered in login form CodeIgniter


My problem seems to be passing the $data['error'] array to the view as I get no errors when wrong data is enetered:

My form posts to this controllers index function:

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);

}

Here is the models process function:

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

In the view I access the form errors with echo validation_errors();

If I submit the form with wron data, I get no error message... Why?

Regards


Solution

  • Update controller functions as shown.

     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 error similar as shown below.

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