Feature/form request strict mode#59430
Conversation
|
very useful |
|
Very Very Good |
|
Can you double check your statement about |
You're absolutely right — $request->validated() is already bounded to declared fields, that bullet point was inaccurate. I've updated the description accordingly. |
|
This would be really helpful, especially consuming APIs in a development environment. I make more typos than I’d like to admit when integrating with APIs, and having unknown fields fail fast would save a lot of debugging time. |
That's exactly the use case this was designed for! The ! app()->isProduction() pattern in the usage example is intentional for this reason — strict in development so typos like emial instead of email fail immediately rather than silently passing through and causing confusing behavior downstream, while staying lenient in production to avoid breaking live traffic. Glad it resonates. |
Have you considered also adding an attribute? // Opt this specific request out of the global strict mode
#[FailOnUnknownFields]
class PublicWebhookRequest extends FormRequest
{
public function rules(): array { ... }
} |
Why not? That sounds good. Thank you for your feedback. |
* strict mode for validation * delete validation.php file * formated * formatting * formatting * more tests --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
Summary
This PR introduces a global strict mode for
FormRequestthat rejects any input field not explicitly declared in therules()method. The behavior can be toggled application-wide fromAppServiceProvider, just likeModel::shouldBeStrict()in Eloquent.Motivation
Laravel's
FormRequestvalidates the content of known fields but silently ignores fields that were never declared inrules(). In practice this means:is_admin,role,balance) alongside a legitimate request, and they will pass validation without a single warning.$request->validated()correctly bounds its output to declared fields, raw request access via$request->all()or$request->input()— common in middleware, service classes, or when the request object is passed directly — allows undeclared fields to flow through silently.Usage
Option 1 — Global, via
AppServiceProvider:Option 2 — Per-class override:
Backward Compatibility
Fully backward compatible. The flag defaults to
false, meaning no existing behavior changes unless the developer explicitly callsFormRequest::failOnUnknownFields()or sets$failOnUnknownFieldson a specific request class.