JustAMartin JustAMartin - 1 year ago 101
PHP Question

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

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...

Answer Source

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());
// ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download