我已经创造了[email protected]
密码路线,观点和方法,并[email protected]
如何在Laravel 5中验证当前,新的和新的密码确认?
此刻,如果我填写new_password
领域,它被散列并提交到数据库中正确的,那么我就可以使用新密码登录。
但我需要能够验证new_password
和new_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>
谢谢你这些有用的提示。我正在使用'confirmation'而不是'same:new_password' - 只是改变了它,它工作。我将使用'Hash :: check()'来验证当前的密码。 – Halnex
很高兴它帮助:) – Sid
在确认字段中使用另一个好的技巧:'确认'验证规则<[link](https://laravel.com/docs/5.4/validation#rule-confirmed)>。 您的实际规则将只是一行,如下所示: ''new_password'=>'required | confirmed''。确认字段名称应该是'new_password_confirmation'。 – Corner