Welcome Guest, Not a member yet? Register   Sign In
Cant seem to find a way to get my forum Validation working
#1

[eluser]Unknown[/eluser]
Hi all,

I'm trying to create a simple login page with a members area. The problem i'm having comes when i try to validate the login form and send back errors. I've read the form validation user guide and what i could find online but cant seem to get it working my way.

Being a login form there's 3 validations required. User not empty / pass not empty / user&pass; exist in the db if the first 2 are true.

All nice and said , now the problem. I tried tackling this two ways.

First i tried to inject an error directly in the error list that gets displayed on the form view. failed

Second i made a custom validation and attached it to the username field . Now the problem is that the custom validation is NOT triggered when the username field is filled. (i cant get "test" to show when the username & pass fields are not empty but "above custom validation" shows )



Here is my code: ( please keep in mind that i'm only using one controller for the code below and i'm extending CI_Controller and not CI_Form_Validation )
Code:
function custom_validation()
{
    echo 'test';
    $this->load->model('adm_users_model');
    $login_val = $this->adm_users_model->validateLogin();

    if ($login_val)
    {

        $data = array(
            'user' => array(
                'is_logged' => 1,
                'username' => $login_val->username,
            )
        );

        $this->session->set_userdata($data);

        return true;
    }
    else
    {    
    
        $this->form_validation->set_message('custom_validation', 'Username/Password combination is invalid');
        return false;
    }
}


public function adminLoginAction()
{
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');
    if ($this->input->post('username') != '' and $this->input->post('password') != '')
    {
        echo 'above custom validation';
        $this->form_validation->set_rules('username', 'Username/Password', '_callback_custom_validation');
    }

    $submit = $this->input->post('doLogin');
    if (isset($submit))
    {
            $this->form_validation->run();
            $this->index();

    }
    else
    {    
        $this->index();
    }
}

Any help would be greatly apreciated .. i'm at the chain smoking head banging stage right about now Smile
#2

[eluser]Victor Michnowicz[/eluser]
Just glancing at your code I noticed that in your form validation rules you have "_callback_custom_validation". There should be no underscore at the beginning. It should be "callback_custom_validation".
#3

[eluser]Unknown[/eluser]
OMFG it worked Smile , thank you , thank you ,thank you . I love this forum ! Big Grin
#4

[eluser]InsiteFX[/eluser]
Using an underscore before a method/function name in CI makes it private.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB