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
#2

[eluser]transition[/eluser]
I'd be interested in knowing if there's a solution to this as well since i'm in the same boat.
#3

[eluser]mattpointblank[/eluser]
The one thing I can think of is using Flashdata (http://ellislab.com/codeigniter/user-gui...sions.html) but I feel like this is a bit hacky, in principle.
#4

[eluser]SardiorDragon[/eluser]
In this situation I would think using Flashdata is the way to go. The error messages will only be generated using the Form Validation if they are set before the call to run and found during the call to run. The only way to set the error message for the password is to create a call back to check it, which you might not be able to do because you need the username to check which password to check.

I ran into the same situation and decided to just use Flashdata. It works great and solves the issue.




Theme © iAndrew 2016 - Forum software by © MyBB