Welcome Guest, Not a member yet? Register   Sign In
showing login/signup and validation errors together
#1

[eluser]notset[/eluser]
After validating a form function uses 'username' and 'password' input to login. If login unsuccessful - it needs to show error. How to link such error together with form validation?

I can't use callback on field validation rules since function requires more than one argument.

Also, I need to use filtered data ($this->input->post('username') - is not, yes?), so I don't have any idea how find the solution.
#2

[eluser]Thorpe Obazee[/eluser]
Code:
<?php echo form_error('username'); ?>
<?php echo form_error('password'); ?>

sorry, I might have misunderstood the question.
#3

[eluser]notset[/eluser]
No. It's hard to explain...

For example, this is my username callback (form validation rule):

Code:
function _username_check($username)
{
    if ($this->user_model->is_unique('username', $username))
    {
        return TRUE;
    }
    else
    {
        $this->form_validation->set_message('_username_check', $this->lang->line('username_exists'));
        return FALSE;
    }
}

And such would be my login function:

Code:
function _login($username, $password)
{
    if ($this->user_model->login($username, $password))
    {
        return TRUE;
    }
    else
    {
        // Won't work, because I can't have mult-argument callback as a validation rule! How to fix that?
        $this->form_validation->set_message('_login', $this->lang->line('invalid_login'));
        return FALSE;
    }
}
#4

[eluser]zutis[/eluser]
Make a callback function for the username only - then in the callback function use the post vars, something like this maybe:

Code:
function login_check($username)
    {
        //  Are login details correct ?
        // I would writte a model for the user that contains a function called login. This should return true or false.
        $logged_in = $this->user->login($this->input->post(‘username’), $this->input->post(‘password’));
        
        if ($logged_in)
        {
            return TRUE;
        }
        else
        {
            $this->form_validation->set_message('username_check', 'Login failed.');
            return FALSE;    
        }
    }
#5

[eluser]notset[/eluser]
[quote author="zutis" date="1238595512"]Make a callback function for the username only - then in the callback function use the post vars[/quote]
But post vars wouldn't be filtered by form validation class, or would it? However, that seems to be the best solution... Thank you.
#6

[eluser]zutis[/eluser]
Not sure what you mean by "filtered". It should work though.
#7

[eluser]TheFuzzy0ne[/eluser]
I agree. The solution will work. Fields should be validated individually, although custom callbacks can use other post fields, even though they are still only validating one thing.

I'd like to point out, however, that technically, logging in is not validation. I have my auth library do that for me, and my validation callbacks link to my auth library, which does allows me to check if the username is valid, and if the password matches.




Theme © iAndrew 2016 - Forum software by © MyBB