2016-06-01 123 views
8

我已经创造了[email protected]密码路线,观点和方法,并[email protected]如何在Laravel 5中验证当前,新的和新的密码确认?

此刻,如果我填写new_password领域,它被散列并提交到数据库中正确的,那么我就可以使用新密码登录。

但我需要能够验证new_passwordnew_password_confirm以确保它们相同并验证用户的当前密码。

我该怎么做?

编辑︰我加了$this->validate的方法,但现在我不断得到错误The password confirmation confirmation does not match.即使他们匹配,因为我使用一个简单的密码。另外我想我需要手动检查当前的密码,因为validator不会为我做。

public function getProfilePassword(Request $request) { 
    return view('profile/password', ['user' => Auth::user()]); 
} 

public function postProfilePassword(Request $request) { 
    $user = Auth::user(); 

    $this->validate($request, [ 
     'old_password'   => 'required', 
     'password'    => 'required|min:4', 
     'password_confirmation' => 'required|confirmed' 
    ]); 

    $user->password = Hash::make(Input::get('new_password')); 
    $user->save(); 
} 

这是视图

<form action="{{ route('profile/updatepassword') }}" method="post" enctype="multipart/form-data"> 
    <div class="form-group"> 
      <label for="name">Current Password</label> 
      <input type="password" name="old_password" class="form-control" id="old_password"> 
    </div> 
    <div class="form-group"> 
      <label for="name">Password</label> 
      <input type="password" name="password" class="form-control" id="password"> 
    </div> 
    <div class="form-group"> 
      <label for="name">New Password</label> 
      <input type="password" name="password_confirmation" class="form-control" id="password_confirmation"> 
    </div> 
    <button type="submit" class="btn btn-primary">Change Password</button> 
    <input type="hidden" value="{{ Session::token() }}" name="_token"> 
</form> 

回答

29

有一个Hash::check()功能,它允许您通过检查用户输入旧密码是否正确。

usage

if (Hash::check("param1", "param2")) { 
//add logic here 
} 

param1 - user password that has been entered on the form 
param2 - old password hash stored in database 

如果旧密码输入正确,它将返回true,你可以添加你的逻辑相应

new_passwordnew_confirm_password是一样的,你可以在形式上要求添加您的验证像

'new_password' => 'required', 
'new_confirm_password' => 'required|same:new_password' 
+0

谢谢你这些有用的提示。我正在使用'confirmation'而不是'same:new_password' - 只是改变了它,它工作。我将使用'Hash :: check()'来验证当前的密码。 – Halnex

+0

很高兴它帮助:) – Sid

+6

在确认字段中使用另一个好的技巧:'确认'验证规则<[link](https://laravel.com/docs/5.4/validation#rule-confirmed)>。 您的实际规则将只是一行,如下所示: ''new_password'=>'required | confirmed''。确认字段名称应该是'new_password_confirmation'。 – Corner

3

您可以添加confirmed,因为它是确认ol d密码。 而'required|confirmed'更改为'required|same:password'比较passwordpassword confirmation

'old_password' => 'required|confirmed', 'password' => 'required|min:4', 'password_confirmation' => 'required|same:password'

祝你好运!

+2

验证下的验证必须具有foo_confirmation的匹配字段。例如,如果验证中的字段是密码,则输入中必须存在匹配的password_confirmation字段。 –

+0

根据文档,这是错误的:https://laravel.com/docs/5.1/validation#rule-confirmed – Jonjie

0

你可以通过创建一个自定义验证规则来做到这一点(对于这个例子,我使用current_passwordnew_password作为输入名称)。

把这个AppServiceProvider::boot()

Validator::extend('current_password', function ($attribute, $value, $parameters, $validator) { 
    $user = User::find($parameters[0]); 

    return $user && Hash::check($value, $user->password); 
}); 

现在你可以使用在你的控制器如下:

$user = auth()->user(); // or pass an actual user here 

$this->validate($request, [ 
    'current_password' => 'required_with:new_password|current_password,'.$user->id, 
]);