CodeIgniter Forums
Using Validation Class to validate Username and Password - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using Validation Class to validate Username and Password (/showthread.php?tid=15911)



Using Validation Class to validate Username and Password - El Forum - 02-18-2009

[eluser]chadsaun[/eluser]
I'm new to Codeigniter and need some help to figure out the best way to validate the username and password.

This is what I have so far, but I want to setup some validation rule for validating if the username and password were found in the DB so that if it wasn't they'll see an error message.

Code:
function authorize() {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|max_length[50]');
        
        $authenticated = false;
        
        $query = $this->db->get_where('safety_users', array('username' => $_POST['username'], 'password' => $_POST['password']));
        if ($query->num_rows() > 0) {
            $authenticated = true;
        }
        
        if ($this->form_validation->run() == false || !$authenticated) {
            $this->load->view('login_view');
        } else {
            $this->load->view('products_view');
        }
    }



Using Validation Class to validate Username and Password - El Forum - 02-18-2009

[eluser]Mark Skilbeck[/eluser]
You want to check out callbacks.

Code:
$this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[50]|xss_clean|callback_username_exists');
$this->form_validation->set_rules('password', 'Password', 'trim|required|max_length[50]');

/** your callback **/
public function username_exists ( )
{
    if ( ! $check_for_username )
    {
        $this->form_validation->set_message('username_exists', 'Username does not exist');
        return false;
    }
    else
    {
        return true;
    }
}



Using Validation Class to validate Username and Password - El Forum - 02-18-2009

[eluser]chadsaun[/eluser]
I looked at callbacks, but from what I understand the callback will only be passed the form field it was attached to. So you couldn't get both the username and password.

How would I do this with a callback?


Using Validation Class to validate Username and Password - El Forum - 02-19-2009

[eluser]TheFuzzy0ne[/eluser]
I don't understand what you mean.


Using Validation Class to validate Username and Password - El Forum - 02-19-2009

[eluser]Colin Williams[/eluser]
You can get the other fields' value in the callback with $this->input->post('other_field')