How to Use Confirmed Rule in Laravel for Password Confirmation
In Laravel, the
confirmed validation rule checks if a field has a matching field with the same name plus _confirmation suffix. For example, password must match password_confirmation. Use it in your validation rules array to enforce this check easily.Syntax
The confirmed rule expects a field to have a matching confirmation field with the same name plus _confirmation. For example, if you validate password with confirmed, Laravel looks for password_confirmation in the input.
Use it in your validation rules like this:
'field_name' => 'confirmed'This means field_name and field_name_confirmation must have the same value.
php
'password' => 'required|string|confirmed'
Example
This example shows how to validate a password and its confirmation in a Laravel controller method. If the two fields don't match, validation fails and returns an error.
php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class RegisterController extends Controller { public function store(Request $request) { $validated = $request->validate([ 'password' => 'required|string|min:8|confirmed', ]); // If validation passes, you can proceed return 'Password confirmed successfully!'; } } // Example input: // password: secret123 // password_confirmation: secret123 // If password_confirmation differs, validation fails with an error message.
Output
Password confirmed successfully!
Common Pitfalls
- Not including the
_confirmationfield in the form input will cause validation to fail. - Using a different name for the confirmation field than the expected
fieldname_confirmationwill not work. - Forgetting to add the
confirmedrule in validation means no confirmation check happens.
Example of a common mistake and the fix:
php
<?php // Wrong: confirmation field named 'password_confirm' instead of 'password_confirmation' $request->validate([ 'password' => 'required|string|confirmed', ]); // Input fields: 'password' and 'password_confirm' => validation fails // Right: confirmation field named 'password_confirmation' $request->validate([ 'password' => 'required|string|confirmed', ]); // Input fields: 'password' and 'password_confirmation' => validation passes if values match
Quick Reference
Summary tips for using the confirmed rule:
- The confirmation field must be named exactly as
fieldname_confirmation. - Use
confirmedin the validation rules for the original field. - Commonly used for passwords, emails, or any sensitive input requiring double entry.
- Validation fails if confirmation field is missing or values differ.
Key Takeaways
Use the confirmed rule to ensure two input fields match, like password and password_confirmation.
The confirmation field must be named with _confirmation suffix exactly.
Validation fails if the confirmation field is missing or values don't match.
Add confirmed to the original field's validation rules to enable this check.
Commonly used for password confirmation in registration or password reset forms.