Welcome Guest, Not a member yet? Register   Sign In
Using Validation Class to validate Username and Password
#1

[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');
        }
    }
#2

[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;
    }
}
#3

[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?
#4

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

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




Theme © iAndrew 2016 - Forum software by © MyBB