Welcome Guest, Not a member yet? Register   Sign In
Validation
#1

[eluser]schnoodles[/eluser]
Hello i am currently building a usersystem and i am up to the part where you check to see if the username and password is correct. i was wondering if there is a proper way to throw errors for functions to make them look like it was a validation error.

So i could do like

Code:
if ( $validation->run() == true ) {
if ( !$user->exists ) { //do like a validation error }
else {

}
}

Anyone know what the best way to accomplish what i am trying to do is ?
#2

[eluser]xwero[/eluser]
I do something like
Code:
$errors = '';
if(!$validation->run()){ $errors = $this->validation->error_string; }
if(!$user->exists){ $errors = 'User doesn\'t exist.'; }
if($errors == '')
{
    // error code
}
else
{
   // success code
}
#3

[eluser]schnoodles[/eluser]
Ahh that is really nice, i was just wondering if i can add an error message from $user->exists into $this->validation->error_string; at all. So it actually looks part of my user system.
#4

[eluser]xwero[/eluser]
You could use the extended validation library talked about on the forum that enables you to write your own validators but checking if the user exists is not a common task so i don't know why you should need to have this error message as a validator.
#5

[eluser]xwero[/eluser]
as an alternative you could make a callback function where you do the user exists check and you set a custom message but that is stretching the validation library a bit
#6

[eluser]schnoodles[/eluser]
Well that is what i want to do, but the problem is that i need to check the db with username AND password, and not just one or the other.

Is there a way to give a normal error message but add it into the validators error message system?
#7

[eluser]xwero[/eluser]
I don't know if it is going to work but you could extend the validation library with a user_exists rule
Code:
MY_Validation extends CI_Validation
{
    function user_exists($user,$password)
    {
       // database code
       return (exists)?TRUE:FALSE;
    }
}
in the controller you would have to do something like
Code:
$rules['username'] = "user_exists[password]";
Then you have to add the user_exists message to the validation language file.
Again i don't know if it works but if it does it seems to be a better way to extend the rules than using callbacks. The only drawback is the compatibility with future versions of the CI validation class.
#8

[eluser]schnoodles[/eluser]
Yah this is abit annoying, im going to have to delve into the code see if i can just add errors manually to the error_string because thats all i really want to do.
#9

[eluser]Michael Ekoka[/eluser]
Hey Schnoodles, I don't know if I properly understand what you're asking, but this is what I would do:

- to create callback methods in your controller to check uniqueness and password validity:

Code:
function _is_unique($data){
    // set the validator error message here
     $this->validation->set_message('_is_unique', 'The %1$s is already taken.');

    // do your normal queries to the database here to check if something is unique.
    // If not return false. The validator doesn't care what happens here, it just cares
    // to see if the callback returns false, in which case the previously set error
    // message is displayed.

}

function _valid_password($username){
    // set the validator error message here
     $this->validation->set_message('_valid_password', 'The Password is incorrect.');
    
    // do your normal authentication routines here
}

Alternatively, you can look for the language file /language/english/validation_lang.php and add your error messages there. Following the format that you find in the file, just put the name of the callback method and the custom error message.


- Now to authenticate and use the validation mechanism you would do this upon submit:
Code:
$rules['username'] = 'callback__valid_password';
$fields['username'] = 'User name';
$fields['password'] = 'Password';
$this->validation->set_rules($rules);
$this->validation->set_fields($fields);
$valid  = $this->validation->run();
#10

[eluser]schnoodles[/eluser]
Not exactly, i cant use a standard callback because i want to compare USERNAME and PASSWORD in one hit. I have found a way around it although i am not sure if its the best way.

in Password Rules i use login_match[username] and the code i have is

Code:
<?php

    class MY_Validation extends CI_Validation {

        public function My_Validation()
        {
            parent::CI_Validation();
        }
        
       /*
        * Add login_match[username] to your
        * password rules.
        */
      
        public function login_match($str,$field)
        {
            if ( ! isset($_POST[$field]))
            {
                return FALSE;
            }
        
            $this->CI->load->model('jb_usermodel');
            $user = new jb_usermodel();
            
            if ( !$user->hasUser($_POST[$field],$str) && sizeof($this->_error_array) == 0)
            {
                $this->set_message('login_match', 'Your username or password is incorrect');
                return FALSE;
            }
            
            return TRUE;
        }
    
    }

?>

Hopefully that will give everyone a better idea of what i am trying to accomplish there. If anyone has a better way of doing this maybe without subclassing the Validation class i would love to hear it.




Theme © iAndrew 2016 - Forum software by © MyBB