I am making the following call to my API routes using Postman everything works fine until I force a validation error. the error object returned doesn't contain the full list of validation errors instead it only has something like this:
{"message":"The given data was invalid.","errors":{"email":["validation.email"],"password":["validation.min.string"]}}
What I want is that instead of "validation.min.string" it should return the full error message.
Here is my controller method:
public function __invoke(Request $request)
{
$credentials = $request->validate([
'email' => 'required|email|max:255',
'password' => 'required|min:8'
]);
$credentials['status'] = true;
if (Auth::attempt($credentials)) {
return response()->json('login success!');
} else {
return response()->json('login failure!');
}
}
Here is the screen shot of the postman request
CodePudding user response:
As par my comment:
- Ensure the file
resources/lang/en/validation.php
exists - If so, Ensure the keys
email
andmin
exists
As a side note:
You can move the folder lang
at the top level and it will still work.
So resources/lang/en
would become lang/en
CodePudding user response:
create new FormRequest class and override below method in
protected function failedValidation(Validator $validator) {
$response = [
'status' => false,
'message' => $validator->errors()->first(),
];
throw new HttpResponseException(response()->json($response, 422));
}
Or use this code :
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
// your validation rules
])->stopOnFirstFailure(true);
$validator->validate();
it will return only single validation message