Search code examples
laravellaravel-validationlaravel-authenticationlaravel-api

Laravel 8 API call not returning validation errors correctly


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

Screenshot here


Solution

  • As par my comment:

    1. Ensure the file resources/lang/en/validation.php exists
    2. If so, Ensure the keys email and min 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