Laravel使用哈希作为验证器 [英] Laravel use Hash as Validator

查看:112
本文介绍了Laravel使用哈希作为验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hash::check()来检查当前密码和输入的密码.如果要检查此操作,我会用它

i'm using Hash::check() to check current password with entered password. i use this if for check 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!');
}

如何将其用作validator?例如在此rules

how to use that as validator ? for example in this rules

$rules = array(
            'name'        => 'required|alpha',
            'family'      => 'required',
            'email'       => 'required|email',
            'currPassword'=> 'required',
            'password'    => 'required|confirmed',
            'password_confirmation'=>'required',
        );

推荐答案

您可以在验证器中添加自定义规则:

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')

因此,如果对此规则的验证失败,那么您将在视图中看到此错误消息,并且可以使用$errors->first('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!' );

因此,在验证期间,您必须使用以下命令传递$messages数组:

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

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

检查文档上的自定义验证规则.

这篇关于Laravel使用哈希作为验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆