How to return custom error message from controller method validation
Asked Answered
G

5

43

How to return a custom error message using this format?

$this->validate($request, [
  'thing' => 'required'
]);
Groan answered 16/10, 2016 at 5:35 Comment(0)
R
77

to get custom error message you need to pass custom error message on third parameter,like that

$this->validate(
    $request, 
    ['thing' => 'required'],
    ['thing.required' => 'this is my custom error message for required']
);
Reach answered 16/10, 2016 at 8:17 Comment(2)
how can we add the custom message for two dimensional array?Ultramicroscopic
I am not sure why this isn't made clearer in the documentation. It's a simple approach and works with Laravel 7. Thanks!Consequential
A
50

For Multiple Field, Role and Field-Role Specific Message

$this->validate(
        $request, 
        [   
            'uEmail'             => 'required|unique:members',
            'uPassword'          => 'required|min:8'
        ],
        [   
            'uEmail.required'    => 'Please Provide Your Email Address For Better Communication, Thank You.',
            'uEmail.unique'      => 'Sorry, This Email Address Is Already Used By Another User. Please Try With Different One, Thank You.',
            'uPassword.required' => 'Password Is Required For Your Information Safety, Thank You.',
            'uPassword.min'      => 'Password Length Should Be More Than 8 Character Or Digit Or Mix, Thank You.',
        ]
    );
Ayana answered 7/5, 2017 at 9:58 Comment(1)
thanks man, it helped me to add custom error message in laravel 5.5... :-)Zooid
G
4

https://laravel.com/docs/5.3/validation#working-with-error-messages

$messages = [
    'required' => 'The :attribute field is required.',
];

$validator = Validator::make($input, $rules, $messages);

"In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file."

Girardi answered 16/10, 2016 at 8:28 Comment(0)
I
4

Strangely not present in the documentation, you can specify the first parameter as the validation rules & the second parameter as the message format directly off of the Illuminate/Http/Request instead of invoking $this or the Validator class.

public function createCustomer(Request $request) 
{

  # Let's assume you have a $request->input('customer') parameter POSTed.

  $request->validate([
    'customer.name' => 'required|max:255',
    'customer.email' => 'required|email|unique:customers,email',
    'customer.mobile' => 'required|unique:customers,mobile',
  ], [
    'customer.name.required' => 'A customer name is required.',
    'customer.email.required' => 'A customer email is required',
    'customer.email.email' => 'Please specify a real email',
    'customer.email.unique' => 'You have a customer with that email.',
    'customer.mobile.required' => 'A mobile number is required.',
    'customer.mobile.unique' => 'You have a customer with that mobile.',
  ]);

}
Ikhnaton answered 17/10, 2022 at 8:59 Comment(0)
Z
1

You need to first add following lines in view page where you want to show the Error message:

<div class="row">
        <div class="col-md-4 col-md-offset-4 error">
            <ul>
                @foreach($errors->all() as $error)
                    <li>{{$error}}</li>
                @endforeach
            </ul>
        </div>
    </div>

Here is a demo controller by which error message will appear on that page:

public function saveUser(Request $request)

 {
     $this->validate($request,[
        'name' => 'required',          
        'email' => 'required|unique:users',          
        ]);
  $user=new User();
  $user->name= $request->Input(['name']);
  $user->email=$request->Input(['email']);
  $user->save();
  return redirect('getUser');
 }

For details You can follow the Blog post. Besides that you can follow laravel official doc as well Validation.

Zacatecas answered 16/10, 2016 at 6:43 Comment(3)
I'm talking about a custom message like can be done here... $messages = [ 'required' => 'The :attribute field is required.', ]; $validator = Validator::make($input, $rules, $messages);Groan
but using $this->validate()Groan
@Groan exactly the same. Signature of validate in controller (via ValidatesRequests class) is void validate(Request $request, array $rules, array $messages = array(), array $customAttributes = array())Archaism

© 2022 - 2024 — McMap. All rights reserved.