Tôi đã tạo ra con đường mật khẩu, xem và phương pháp trong [email protected]
và [email protected]
Làm cách nào để xác nhận xác nhận mật khẩu mới, mới và mới trong Laravel 5?
Tại thời điểm này, nếu tôi điền thông tin vào lĩnh vực new_password
, nó được băm và nộp cho cơ sở dữ liệu một cách chính xác, sau đó tôi có thể đăng nhập với mật khẩu mới .
Nhưng tôi cần có khả năng xác thực new_password
và new_password_confirm
để đảm bảo chúng giống nhau và xác thực mật khẩu hiện tại của người dùng.
Tôi có thể làm như thế nào?
EDIT: Tôi đã thêm $this->validate
vào phương pháp, nhưng bây giờ tôi tiếp tục gặp lỗi The password confirmation confirmation does not match.
mặc dù chúng khớp với nhau khi tôi đang sử dụng mật khẩu đơn giản. Ngoài ra tôi nghĩ rằng tôi cần phải kiểm tra mật khẩu hiện tại theo cách thủ công như validator
sẽ không làm điều đó cho tôi.
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();
}
Và đây là quan điểm
<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>
Cảm ơn bạn cho những lời khuyên hữu ích. Tôi đã sử dụng 'xác nhận' thay vì' cùng: new_password' - chỉ cần thay đổi nó và nó đã làm việc. Và tôi sẽ sử dụng 'Hash :: check()' để xác thực mật khẩu hiện tại. – Halnex
vui mừng vì nó đã giúp :) – Sid
Một mẹo hay khác để sử dụng trong trường xác nhận: quy tắc xác thực 'đã xác nhận' <[link] (https://laravel.com/docs/5.4/validation#rule-confirmed)>. Quy tắc thực tế của bạn sẽ chỉ là một dòng, như sau: '' new_password '=>' required | confirmed''. Và tên trường xác nhận phải là 'new_password_confirmation'. – Corner