0
votes

Request Class

class LoginRequest extends FormRequest
{
    public function wantsJson() {
        return true;
    }   

    public function authorize() {
        return true;
    }

    public function rules() {
        $validators = [
            'email'    =>  'required',
            'password' =>  'required'
        ];
        return $validators;
    }

    public function failedValidation(\Illuminate\Contracts\Validation\Validator $validator) {
        if($validator->fails()) {
            //print_r($validator->errors());
            //die();

        }
        return parent::failedValidation($validator);
    }
}

I have an api written in Laravel. I am trying to test the validation through Postman extension. When I submit some values of email and password, it works. I get the message that the credentials exists or not.

In case, I don't submit the values, then, there is no json messagebag returned.

I can confirm that there are validation error messages in MessageBag. Here is the screenshot. If the screenshot is not clear then please click it see it.

Another strange things is that the status code returned is 200

Please let me know if you need more info

enter image description here

1

1 Answers

1
votes

In my situation I setup my Laravel API like this.

in my App\Exceptions\Handler

public function render($request, Exception $exception)
    {
        // return parent::render($request, $exception);

        $rendered = parent::render($request, $exception);

        if ($exception instanceof ValidationException) {
            $json = [
                'error' => $exception->validator->errors(),
                'status_code' => $rendered->getStatusCode()
            ];
        } elseif ($exception instanceof AuthorizationException) {
            $json = [
                'error' => 'You are not allowed to do this action.',
                'status_code' => 403
            ];
        }
        else {
            // Default to vague error to avoid revealing sensitive information
            $json = [
                'error' => (app()->environment() !== 'production')
                    ? $exception->getMessage()
                    : 'An error has occurred.',
                'status_code' => $exception->getCode()
            ];
        }

        return response()->json($json, $rendered->getStatusCode());
    }

also import this on top

use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;

It helps to format the errors into JSON format.

My LoginRequest looks like this (simple)

class LoginRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required|email',
            'password' => 'required'
        ];
    }
}