How to return custom error message from controller method validation
Solution 1
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']
);
Solution 2
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.',
]
);
Solution 3
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."
Solution 4
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.
cmac
I'm a Software Developer working mostly with PHP, JavaScript, Python, Laravel, NodeJS, Neo4J, ElasticSearch, MySQL, MongoDB, etc...
Updated on July 05, 2022Comments
-
cmac almost 2 years
How to return a custom error message using this format?
$this->validate($request, [ 'thing' => 'required' ]);
-
cmac over 7 yearsI'm talking about a custom message like can be done here... $messages = [ 'required' => 'The :attribute field is required.', ]; $validator = Validator::make($input, $rules, $messages);
-
cmac over 7 yearsbut using $this->validate()
-
Skysplit over 7 years@cmac exactly the same. Signature of
validate
in controller (viaValidatesRequests
class) isvoid validate(Request $request, array $rules, array $messages = array(), array $customAttributes = array())
-
sujivasagam over 7 yearshow can we add the custom message for two dimensional array?
-
mhk almost 6 yearsthanks man, it helped me to add custom error message in laravel 5.5... :-)
-
Sheldon Scott about 4 yearsI am not sure why this isn't made clearer in the documentation. It's a simple approach and works with Laravel 7. Thanks!