cmac
cmac

Reputation: 3268

How to return custom error message from controller method validation

How to return a custom error message using this format?

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

Upvotes: 43

Views: 94827

Answers (5)

Grant
Grant

Reputation: 6309

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

}

Upvotes: 4

Md. Noor-A-Alam Siddique
Md. Noor-A-Alam Siddique

Reputation: 1077

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

Upvotes: 50

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5443

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

Upvotes: 79

user634545
user634545

Reputation: 9419

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

Upvotes: 4

Hola
Hola

Reputation: 2233

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.

Upvotes: 1

Related Questions