ユーザーが自分のアカウントパスワードを変更できる機能をアプリケーションに追加しようとしています。私は3つのフィールドがあり、私の見解は次のようになります。まずLaravel 5 After Validationフックを使用して配列にエラーを追加する
<form class="form" role="form" action="{{ url('users/updatePassword') }}" method="post">
{{ csrf_field() }}
<div class="form-group label-floating {{ $errors->has('oldpassword') ? 'has-error' : '' }}">
<label class="control-label" for="oldpassword">Old Password</label>
<input type="password" name="oldpassword" class="form-control">
@if ($errors->has('oldpassword'))
<span class="help-block">
<strong>{{ $errors->first('oldpassword') }}</strong>
</span>
@endif
</div>
<div class="form-group label-floating {{ $errors->has('newpassword') ? 'has-error' : '' }}">
<label class="control-label" for="newpassword">New Password</label>
<input type="password" name="newpassword" class="form-control">
@if ($errors->has('newpassword'))
<span class="help-block">
<strong>{{ $errors->first('newpassword') }}</strong>
</span>
@endif
</div>
<div class="form-group label-floating">
<label class="control-label" for="newpassword_confirmation">Confirm Password</label>
<input type="password" name="newpassword_confirmation" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-raised btn-primary">Change</button>
</div>
</form>
、私はすべてのフィールドが完全に満たされ、そのために私はValidator
を使用しているかどうかを確認します。そしてoldpassword
がデータベースと一致するので、if (Auth::attempt(array('password' => $request->oldpassword)))
の条件を使用しているかどうかを確認してください。私はlaravel 5.2のドキュメントでAfter Validation hookも見つけました。何が間違っているのかわかりませんが、間違ったパスワードを入力したときにoldpassword
フィールドを検証しないようです。
マイコントローラ:
$validator = Validator::make($request->all(), [
'oldpassword' => 'required|max:255',
'newpassword' => 'required|min:6|max:255|confirmed',
]);
$validator->after(function($validator) use($request) {
if (Auth::attempt(array('password' => $request->oldpassword))) {
$validator->errors()->add('oldpassword', 'Old password dont match in our database.');
}
});
if ($validator->fails()) {
// Toastr
$title = "Oops!";
$message = "Please make sure to fill all required fields.";
$options = [
'progressBar' => false,
'positionClass' => 'toast-top-right',
'timeOut' => 6000,
];
Toastr::error($message, $title, $options);
return redirect()->back()
->withErrors($validator);
} else {
return 'success'; // for testing only
}
これに関するすべてのアイデア?
Arrg以下の使用している場合ので、それは良いでしょう! "!"今働いている。ドキュメンテーション内で 'Hash :: check()'の詳細を読むことができるリンクはありますか? –
はい、https://laravel.com/docs/5.2/hashing –
ありがとうございます! :) .. –