Home » Php » php – How to make Laravel 'confirmed' validator to add errors to the confirmation field?

php – How to make Laravel 'confirmed' validator to add errors to the confirmation field?

Posted by: admin July 12, 2020 Leave a comment


By default, Laravel ‘confirmed’ validator adds the error message to the original field and not to the field which usually contains the confirmed value.

'password' => 'required|confirmed|min:8',

Is there any simple way to extend the validator or use some trick to force it to always show the error on the confirmation field instead of the original field?

If I fail to enter my password twice, the error seems more appropriate to belong the confirmation field and not to the original password field. Or maybe that’s just our UX analyst getting nitpicky…

How to&Answers:

One way to go about it is to use same rule instead of confirmed

// ...

$input = Input::all();

$rules = [
    'password' => 'required|min:8',
    'password_confirmation' => 'required|min:8|same:password',

$messages = [
    'password_confirmation.same' => 'Password Confirmation should match the Password',
$validator = Validator::make($input, $rules, $messages);

if ($validator->fails()) {
    return back()->withInput()->withErrors($validator->messages());
// ...


You should design your form as below;

<input type="password" name="password">
<input type="password" name="password_confirmation">

Quote from Laravel: confirmed
“The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input”

Now, you can design your validation as follow;

        "password" => 'required|confirmed' 


One solution that quickly comes to mind is to just display the password errors on the password_confirmation field.

If that won’t work for you, just label the password_confirmation field as password and the password field as password confirmation so that if there are errors, it will show up near the password_confirmation label rather than the password label.

Otherwise, it’s not difficult to add a your own custom validation method.


            'password2.same'=>'password are not the same password must match same value',
            'password1.min'=>'password length must be greater than 8 characters',
            'password2.min'=>'confirm-password length must be greater than 8 characters',

        $validator=  validator($request->all(), $rules, $error_messages);
        if ($validator->fails()) {
            return redirect('control_pannel/change_password')