我在 UserController@getProfilePassword
和 UserController@postProfilePassword
中创建了密码路由、视图和方法
目前,如果我填写了 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>
原文由 Halnex 发布,翻译遵循 CC BY-SA 4.0 许可协议
有一个
Hash::check()
功能可以让您检查用户输入的旧密码是否正确。usage
如果已正确输入旧密码,它将返回 true,您可以相应地添加逻辑
对于
new_password
和new_confirm_password
相同,您可以在表单请求中添加验证,例如