Skip to content

feat: allow FormRequest validation failures to reach controllers#10300

Open
memleakd wants to merge 2 commits into
codeigniter4:4.8from
memleakd:feat/formrequest-continue-validation-failure
Open

feat: allow FormRequest validation failures to reach controllers#10300
memleakd wants to merge 2 commits into
codeigniter4:4.8from
memleakd:feat/formrequest-continue-validation-failure

Conversation

@memleakd

Copy link
Copy Markdown
Contributor

Description

FormRequest currently assume that a validation failure owns the response: either the default redirect/JSON response, or whatever the request returns from failedValidation().

That works well for many requests, but it is too rigid for server-rendered forms where the invalid response belongs to the controller action. Sometimes the controller already has the route context, loaded resource, page surface, or view composition needed to render the failed form correctly.

This PR keeps the default behavior unchanged. If failedValidation() returns a ResponseInterface, the framework still short-circuits and sends it immediately.

The only new path is that failedValidation() may now return null. In that case, the resolved FormRequest is injected into the controller, and the controller can handle the failed validation itself.

This avoids adding a second flag method or framework-owned error state. The request class can store whatever state the application needs, and the framework only needs to understand one thing: response means stop, null means continue.

Tests cover the default short-circuit behavior, the new controller-handled path, prepared validation data, and authorization still stopping before validation.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Let `FormRequest::failedValidation()` return `null` when an application wants the
controller action to render the invalid response.

- Keep existing ResponseInterface short-circuit behavior
- Preserve authorization failures as immediate responses
- Document controller-handled validation failures

Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Jun 11, 2026
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant