我目前正在尝试为我的用户配置文件更改密码功能,我的所有输入都提交给控制器,但我认为我的功能逻辑可能有问题?
尝试对函数进行转储请求并成功返回转储。但是在围绕验证过程包装验证变量时,不会返回转储。请求重定向回带有表单数据的个人资料页面。
控制器
public function updatePassword(Request $request)
{
$this->validate($request, [
'old_password' => 'required',
'new_password' => 'required|confirmed',
'password_confirm' => 'required'
]);
$user = User::find(Auth::id());
if (!Hash::check($request->current, $user->password)) {
return response()->json(['errors' =>
['current' => ['Current password does not match']]], 422);
}
$user->password = Hash::make($request->password);
$user->save();
return $user;
}
看法
<form method="POST" action="{{ route('update-password') }}">
@csrf
@method('PUT')
<div class="form-group row">
<label for="old_password" class="col-md-2 col-form-label">{{ __('Current password') }}</label>
<div class="col-md-6">
<input id="old_password" name="old_password" type="password" class="form-control" required autofocus>
</div>
</div>
<div class="form-group row">
<label for="new_password" class="col-md-2 col-form-label">{{ __('New password') }}</label>
<div class="col-md-6">
<input id="new_password" name="new_password" type="password" class="form-control" required autofocus>
</div>
</div>
<div class="form-group row">
<label for="password_confirm" class="col-md-2 col-form-label">{{ __('Confirm password') }}</label>
<div class="col-md-6">
<input id="password_confirm" name="password_confirm" type="password" class="form-control" required
autofocus>
</div>
</div>
<div class="form-group login-row row mb-0">
<div class="col-md-8 offset-md-2">
<button type="submit" class="btn btn-primary">
{{ __('Submit') }}
</button>
</div>
</div>
</form>
当“当前密码”错误时,结果应该至少将 422/错误消息返回到控制台,而不仅仅是重定向回查看,当密码正确时,返回 200/成功消息(尚未实现。)到控制台或查看。
qq_花开花谢_0
江户川乱折腾