Welcome Guest, Not a member yet? Register   Sign In
Form validation: Can I pass an error after a form has been validated (eg for login callbacks)?
#1

[eluser]mattpointblank[/eluser]
Hi everyone.

I'm just writing a login script. My form first checks the email exists, is valid, and is in our database. Once a password is supplied (eg, the form validation runs successfully), I run a function to attempt to log the user in using their credentials. I want to return the user to the login form with a message ("incorrect password" or something) - here's my controller:

Code:
<?php
class Login extends Controller {

    function index()
    {
        $this->load->model('Login_model');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_checkEmailExists');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('login');
        } else {
            if($this->Login_model->attemptLogin($this->input->post('email'), $this->input->post('password'))) {
                $this->load->view('formsuccess');
            } else {
                $this->form_validation->set_message('password', 'Password wrong');
                $this->load->view('login');
            }
        }
    }
    
    function checkEmailExists($email)
    {
        $this->load->model('Login_model');
        if($this->Login_model->checkEmail($email)) {
            return TRUE;
        } else {
            $this->form_validation->set_message('checkEmailExists', 'Sorry, this email address does not exist in our database.');
            return FALSE;
        }
    }
}

?>

You can see where I've tried setting an error message ("Password wrong") but this doesn't display - I just get sent back to the login page again, without the message. Is there a way to do this or will I have to make a separate view for failed logins?

Thanks
Matt


Messages In This Thread
Form validation: Can I pass an error after a form has been validated (eg for login callbacks)? - by El Forum - 06-25-2009, 08:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB