fix: refine FormRequest JSON failure detection#10240
Open
memleakd wants to merge 1 commit into
Open
Conversation
- Return default JSON validation errors for JSON bodies or explicit JSON Accept headers - Stop treating XHR/AJAX requests as JSON automatically - Document the behavior and update FormRequest tests Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adjusts the default
FormRequestvalidation failure response so AJAX/XHR requests are not treated as JSON requests automatically.Currently, the default
failedValidation()returns a422JSON response when either:X-Requested-With: XMLHttpRequestThe JSON body case makes sense, but the XHR case feels too broad. A lot of modern apps use AJAX-like requests while still expecting HTML in response. HTMX, Turbo, Unpoly, and similar HTML-over-the-wire approaches can submit forms asynchronously, but the response may still be an HTML form, modal, sheet, or page fragment with validation errors. In those cases, receiving JSON validation errors by default is surprising and makes
FormRequestharder to use for HTML-first applications.With this change, the default JSON response is selected only when the client is explicit about JSON:
Content-Type: application/jsonAccept: application/jsonPlain AJAX/XHR requests now follow the normal web behavior and redirect back with input and validation errors. If an AJAX client expects JSON, it can send
Accept: application/json, while HTML-first apps are not pushed into JSON accidentally.This keeps the default behavior predictable. Apps that need custom failure responses can still override
failedValidation(), just like they can today.The docs were updated to make this explicit, and the tests cover JSON body, JSON
Accept, AJAX-only, AJAX plus JSONAccept, wildcardAccept, and the controller integration path.Checklist: