Reputation: 7042
How to catch 422 Unprocessable entry in laravel 5.5.16 ?
I am getting below error message while I am sending http://127.0.0.1:8000/api/topics
API request.
I would like to customize this error.In this regard I would like to know Which class and function producing this error and their location.
Upvotes: 0
Views: 1033
Reputation: 7289
A 422 Error is thrown when Laravel validates your Request data and the data is wrong, invalid or not processable.
If you are validating your data in an extra Request class, you can add a message
method to override the error messages (further info here):
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'body.required' => 'A message is required',
];
}
If you are validating the data inside your Controller through $request->validate(...)
, you need to create an own Validator class inline with a messages argument (further info here):
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
Upvotes: 1