Welcome Guest, Not a member yet? Register   Sign In
Inexplicable form_validation error
#1

[eluser]Wogan[/eluser]
I'm getting a bizarre error with Form Validation. Maybe someone can point out what must be the obvious, lol.

This is the controller:

Code:
function index()
    {

            $config = array(
                array('field' => 'username','label' => 'Username','rules' => 'required'),
                array('field' => 'password','label' => 'Password','rules' => 'required')
            );
        
        $this->form_validation->set_rules($config);
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('user/login', $data);
        }
        else
        {
            /* Checks if username matches password, redirects as needed */
        }
    }

Ok. So I enter a valid username and password into the fields, it logs me in. I log out, clear all cookies, and I enter another set of credentials. This new set should work - the necessary db records exist, they're following all the validation rules, everything.

Except that they don't work. Instead of passing to the else{}, the validator fails and loads the login view all over again. So I try printing $this->form_validation in event of failure, to see what the errors are.

Surprise! There aren't any - as far as the object itself seems to be concerned, everything passes muster.

So then I go and enter random jibberish into the username/password fields. The validator succeeds, and I get my "username/password invalid" error - the one that's checked for in the else{} block, after form validation.

It's not cookies/sessions in my browser - I've been clearing them on every attempt. It's also not some weird external library - all the controller loads is 'form_validation' in the __construct() bit, and makes use of helper functions like set_cookie() and redirect();

I'll be revisiting this rather fascinating problem in the morning, but I was wondering if anyone might have an idea or two to steer me in the right direction. I mean, how would I go about debugging this if, as far as CI is concerned, there are no bugs?
#2

[eluser]theshiftexchange[/eluser]
I know it shouldnt matter - but try this:

Code:
function index()
    {

            $config = array(
                array('field' => 'username','label' => 'Username','rules' => 'required'),
                array('field' => 'password','label' => 'Password','rules' => 'required')
            );
        
        if ($this->form_validation->run($config) == FALSE)
        {
            $this->load->view('user/login', $data);
        }
        else
        {
            /* Checks if username matches password, redirects as needed */
        }
    }

Also - the more I think about it; the more it seems like a session issue. Have you tried manually clearing the session data from MYSQL (if its store in the db)?
#3

[eluser]Wogan[/eluser]
Yeah I'll try that later.

No, I don't use sessions. There's nothing that could connect the form_validation to the database, far as I know.

The else{} block is structured in such a way that the user is either logged in, or redirected to a completely different controller. So long as the username and password exist, it should work.

Anyway, this is a battle for another day...
#4

[eluser]Dam1an[/eluser]
Comparing it to my login function, the only differance I can see is I don't set my rules in an array, I use
Code:
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
etc

Its worth a try (although I wouldn't get your hopes up :-S)
#5

[eluser]Evil Wizard[/eluser]
you could at least try and get the validation errors
Code:
if(!$this->form_validation->run()) {
    $data['form_errors'] = validation_errors();
        $this->load->view('user/login', $data);
}
I think if it was session or cookie based issue you would still be logged in and wouldn't get the login view




Theme © iAndrew 2016 - Forum software by © MyBB