Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 819
JustAMartin Avatar asked May 11 '15 14:05

JustAMartin


People also ask

How do I display validation error in laravel blade?

How do I show laravel validation errors in blade? You can call any() method on $errors variable to check that there are any validation errors exist in it or not. It returns 1 if any errors exist in $errors variable. After that you can call foreach method on $errors->all() and display message using $error variable.

What is confirmed in laravel validation?

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.

What is the method used for specifying custom messages for validator errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.

How do I create a custom validation rule in laravel?

Custom Validation Rule Using Closures $validator = Validator::make($request->post(),[ 'birth_year'=>[ 'required', function($attribute, $value, $fail){ if($value >= 1990 && $value <= date('Y')){ $fail("The :attribute must be between 1990 to ". date('Y').". "); } } ] ]);


2 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());
}
// ...
like image 108
peterm Avatar answered Nov 09 '22 04:11

peterm


$rules=[
            'username'=>'required|max:20',
            'password1'=>'required|min:8',
            'password2'=>'required|min:8|same:password1',
        ];
        $error_messages=[
            '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')
                        ->withErrors($validator)
                        ->withInput();
       }
like image 29
Ahmed Mahmoud Avatar answered Nov 09 '22 04:11

Ahmed Mahmoud