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 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.
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.
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.
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').". "); } } ] ]);
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());
}
// ...
$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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With