Laravel use Hash as Validator

不羁的心 提交于 2019-12-05 18:37:01

You can add custom rule in 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 the validation fails for this rule then you'll get error messages for this in your view and can access using $errors->first('currPassword'); but you need to pass a custom error message for this custom rule you've created using:

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

So, during validation you have to pass the $messages array using:

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

Check custom validation rules on the documentation.

You also can use a closure validation rule:


    [
        'currPassword' => function ($attribute, $value, $fail) {
            if (! Hash::check($value, $this->user()->password)) {
                $fail('Your current password doesnt match');
            }
        },
        'password' => 'required|min:6|confirmed',
    ];

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!