Laravel uses Hash as Validator

I use Hash::check()to verify the current password with the password entered. i use this if to verify this action

$HashPassowrd = Hash::make(Input::get('password'));

if( ! Hash::check( Input::get('currPassword') , $data->password ) )
{
   return Redirect::to('profile.update')
            ->withErrors('Current Password in Incorrect!');
}

how to use it how validator? for example in thisrules

$rules = array(
            'name'        => 'required|alpha',
            'family'      => 'required',
            'email'       => 'required|email',
            'currPassword'=> 'required',
            'password'    => 'required|confirmed',
            'password_confirmation'=>'required',
        );
+3
source share
1 answer

You can add a custom rule to the validator:

Validator::extend('checkHashedPass', function($attribute, $value, $parameters)
{
    if( ! Hash::check( $value , $parameters[0] ) )
    {
        return false;
    }
    return true;
});

Now you can use this custom rule as:

'currPassword' => 'required|checkHashedPass:' . Input::get('currPassword')

So, if validation fails for this rule, you will get error messages for this in your view and can be accessed with $errors->first('currPassword');, but you need to pass a custom error message for this custom rule that you created using:

$messages = array( 'currPassword.checkHashedPass' => 'Current Password failed!' );

, $messages, :

$validator = Validator::make(Input::all(), $rules, $messages);

.

+6

All Articles