How to validate current, new, and new password confirmation in Laravel 5?
When implementing a profile password change feature in Laravel 5, the common pitfall is assuming that standard validation rules can handle every aspect of security logic automatically. While the confirmed rule is excellent for ensuring the user typed their new password twice correctly on the form, it cannot validate the "current" or "old" password against the database. That comparison requires explicit code.
In your specific case, you've correctly identified that adding $this->validate stops the request from processing blindly, but now you are facing two distinct issues: a confusing error message regarding confirmation matching despite correct input, and the need to manually verify the old password.
The Confirmation Error Mystery
The error message The password confirmation confirmation does not match (note the double word) is almost certainly a typo in your error handling logic or a specific Laravel version quirk, but the root cause of the validation failure usually lies in how the request data is accessed versus what was submitted.
In your controller, you are using Input::get('new_password'). However, your view form submits fields named password and password_confirmation. When you access $request->input('new_password') or pass it to Hash directly, you might be comparing against a non-existent key if the validator has already failed silently or if the input array keys don't align perfectly with what the user submitted.
Ensure that in your controller, you are strictly using the request data as submitted:
$user->password = Hash::make($request->input('password'));
Using Input::get is legacy syntax; while it works in older versions, accessing $request directly is the standard practice in Laravel 5.x. If the validator runs before this line and fails, the request might not reach the save logic as expected. The "confirmation" error often appears because the validator sees the two fields as mismatched if there is any whitespace difference or if the confirmed rule is checking against a key that doesn't exist in the final validated array due to middleware interference.
Validating the Old Password
The Validator class in Laravel does not have a built-in rule like current_password. You must manually check if the hashed password provided by the user matches the one stored in the database. This is a critical security step; without it, a malicious user could change their own password to anything they wanted without knowing their current credentials.
Here is how to implement this robustly within your postProfilePassword method:
public function postProfilePassword(Request $request) {
$user = Auth::user();
// 1. Validate the new password fields first
// We use 'password_confirmation' to ensure they match each other
$validator = Validator::make($request->all(), [
'old_password' => 'required',
'password' => 'required|min:4',
'password_confirmation' => 'same:password' // Explicitly check equality
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
// 2. Manually validate the current password
if (!Hash::check($request->input('old_password'), $user->password)) {
return redirect()->back()
->withErrors(['old_password' => 'The provided current password is incorrect.'])
->withInput();
}
// 3. Update the password only if both checks pass
$user->password = Hash::make($request->input('password'));
$user->save();
return redirect('/profile');
}
Notice the use of Hash::check. This method compares a plain-text string against a hashed value stored in the database. If you used Hash::make on the input and compared it to Hash::make($user->password), you would be hashing the user's password twice, which is unnecessary and incorrect.
Updating Your View
Your view form looks mostly correct, but there are a few adjustments to ensure the data aligns perfectly with the controller expectations:
- Rename fields: Change
new_passwordin your thought process to match the actual input namepassword. The form currently usesname="password"andname="password_confirmation", which is correct for the Laravel 5 validation rules. - Remove
enctype: Unless you are uploading a file with the password change, removeenctype="multipart/form-data". This attribute is only necessary for file uploads and can sometimes cause issues with standard text inputs or CSRF token handling depending on your server configuration. - Feedback Loop: Ensure your view displays Laravel's validation error messages properly using
{{ $errors->first('field_name') }}so users know exactly which field failed.
By separating the confirmation check (handled by Laravel) from the old password check (handled manually), you create a secure and predictable workflow that prevents unauthorized password changes while providing clear feedback to the user.
