Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify validation exception formatting. #20173

Merged
merged 6 commits into from
Jul 19, 2017
Merged

Conversation

taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Jul 19, 2017

This unifies all validation error formatting into the exception handler
and out of Form Request. “Don’t Flash” is typically a global concept
that always applies to a list of fields for all pages in the
applications, so seemed to make sense to specify in one place instead
of each form.

Total control of the Form Request error response can still be gained by
overriding failedValidation and throwing a HttpResponseException with
whatever response you want.

This would mean in Laravel 5.5 all validation JSON responses would be customized in a single location within your application's exception handler:

    /**
     * Convert a validation exception into a JSON response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Validation\ValidationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function invalidJson($request, ValidationException $exception)
    {
        return response()->json([
            'message' => $exception->getMessage(),
            'errors' => $exception->errors(),
        ], 422);
    }

This unifies all validation error formatting into the exception handler
and out of Form Request. “Don’t Flash” is typically a global concept
that always applies to a list of fields for all pages in the
applications, so seemed to make sense to specify in one place instead
of each form.

Total control of the Form Request error response can still be gained by
overriding failedValidation and throwing a HttpResponseException with
whatever response you want.
@taylorotwell taylorotwell merged commit e73fed3 into master Jul 19, 2017
@taylorotwell taylorotwell deleted the validation-responses branch July 20, 2017 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant