43

How to return a custom error message using this format?

$this->validate($request, [
  'thing' => 'required'
]);
0

5 Answers 5

79

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']
);
Sign up to request clarification or add additional context in comments.

2 Comments

how can we add the custom message for two dimensional array?
I am not sure why this isn't made clearer in the documentation. It's a simple approach and works with Laravel 7. Thanks!
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.',
        ]
    );

1 Comment

thanks man, it helped me to add custom error message in laravel 5.5... :-)
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."

Comments

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.',
  ]);

}

Comments

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.

3 Comments

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);
but using $this->validate()
@cmac exactly the same. Signature of validate in controller (via ValidatesRequests class) is void validate(Request $request, array $rules, array $messages = array(), array $customAttributes = array())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.