0

I am working my backend with laravel and I need to validate a password field and the confirmation field, for this I have the following validation rule (a summary):

$rules = [
    'password' => 'required|min:5|confirmed',
    'password_confirmation' => 'required',
];

and in the frontend I am working with vue.js and a validation library vee-validate, which looks more or less like this:

<div class="control-group">
    <input type="password" v-validate="'required'" name="password" class="form-control" v-model="user.password">
    <span class="red" v-show="errors.has('password')">{{ errors.first('password') }}</span>
</div>
<div class="control-group">
    <input type="password" v-validate="'required|confirmed:password'" name="password_confirmation" class="form-control" v-model="user.password_confirmation">
    <span class="red" v-show="errors.has('password_confirmation')">{{ errors.first('password_confirmation') }}</span>
</div>

and modified the library so that it receives and can show the validations that I send from Laravel. All this works correctly, my problem is that Laravel sends the message that the password confirmation is not the same, in the password field:

{"message":"The given data was invalid.","errors":{"password":["The password confirmation does not match."]}}

What is a problem for me because the field that is marked as an error will be the one with the name password, however I think this is not correct since the response message should refer to the password_confirmation field, this is the normal behavior de laravel for this case ?, can I change to the field referred to in the answer?

max
  • 353
  • 3
  • 4
  • 12
  • if password_confirmation must match password, then it must also have the same validation. – TTT Nov 04 '21 at 00:55

1 Answers1

1

This is the general behavior of the confirmed rule. As per documentation,

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.

So, when the confirmation field doesn't have same value as field under validation it will throw error the field. In your case, password field not the confirmation.

You can modify the validation logic like this:

$rules = [
    'password' => 'required|min:5',
    'password_confirmation' => 'required|same:password',
];

This will check confirmation field is same as password or not and throws error in missmatch case. I hope you understand.

Community
  • 1
  • 1
Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84