CoreFormRequest
This document covers theCoreFormRequest base class, which provides automatic sometimes validation rule handling for partial updates (PATCH/PUT requests).
Overview
When building update endpoints in Laravel, you often need to support partial updates where only a subset of fields are sent. This requires addingsometimes to validation rules for non-nullable fields. CoreFormRequest automates this pattern, eliminating boilerplate code and making FormRequest classes more maintainable.
The Problem
WithoutCoreFormRequest, update request classes require manual sometimes declarations:
The Solution
WithCoreFormRequest, you define clean base rules and the class automatically handles sometimes:
CoreFormRequest automatically:
- Prepends
sometimestonameandage(non-nullable fields) - Leaves
emailunchanged (nullable fields don’t needsometimes) - For POST requests, returns the rules as-is (full validation)
Basic Usage
Simple Update Request
name:
With Route Model Binding
You can use Laravel’s dependency injection features with route parameters. Therules() method supports variadic parameters, allowing you to inject route-bound models:
With Custom Authorization
Authorization works the same as standard FormRequest:Advanced Features
Nested Array Rules
CoreFormRequest correctly handles nested array validation rules:
Preventing Automatic sometimes
If you need to override the automatic behavior for specific fields, explicitly add sometimes:
Mixed Validation Rules
Works with all Laravel validation rule types:Custom Request Methods Validation
OverrideisPartialUpdate() if you have custom logic for determining partial updates:
How It Works
Request Flow
- Request arrives → Laravel routes to controller
- FormRequest resolves → Laravel instantiates your CoreFormRequest subclass
validationRules()called → CoreFormRequest intercepts before validation- Method check → Determines if PATCH/PUT (partial update)
- Rule transformation:
- For PATCH/PUT: Prepends
sometimesto non-nullable fields - For POST: Returns rules unchanged
- For PATCH/PUT: Prepends
- Validation runs → Laravel validator processes the transformed rules
Under the Hood
CoreFormRequest overrides the protected validationRules() method from Laravel’s base FormRequest class. This method is called internally by Laravel during validation, giving us the perfect hook to modify rules before they’re processed.
- ✅ You use the standard
rules()method - ✅ Laravel’s dependency injection still works
- ✅ No abstract methods to remember
- ✅ Automatic, transparent modification
Rule Analysis Logic
For each field,CoreFormRequest checks:
- Already has
sometimes? → Skip (no duplication) - Is nullable? → Skip (nullable fields don’t need
sometimes) - Otherwise → Prepend
sometimesto the rules array
String vs Array Rules
The class handles both formats:sometimes is prepended.
Best Practices
When to Use CoreFormRequest
✅ Use for:- Update (PATCH/PUT) endpoints with partial update support
- Resources where not all fields are required in updates
- Reducing boilerplate in Update request classes
- Store (POST) endpoints that always require full validation
- Requests where you need complete control over
sometimeslogic - Simple requests with only nullable fields
Migration Strategy
When migrating existing Update requests:- Change parent class from
FormRequesttoCoreFormRequest - Rename
rules()torules() - Remove manual
sometimesfrom non-nullable fields - Keep nullable fields unchanged
- Test with partial updates
Testing
Test your CoreFormRequest subclasses to ensure proper validation:Common Patterns
Update with Conditional Rules
Update with Dynamic Unique Rules
Update with Custom Messages
API Reference
Abstract Methods
rules()
Define your base validation rules. This method replaces the standard rules() method.
$params- Variadic parameters for dependency injection (e.g., route-bound models)
Protected Methods
isPartialUpdate()
Determines if the request is a partial update (PATCH/PUT).
true for PATCH/PUT requests, false otherwise
Override: You can override this method for custom partial update detection logic.
prependSometimesToNonNullableRules()
Processes rules array and prepends sometimes to non-nullable fields.
$rules- The base rules array
sometimes prepended
hasSometimes()
Checks if a field’s rules already contain sometimes.
isNullable()
Checks if a field’s rules contain nullable.
prependSometimes()
Prepends sometimes to a rules array.
Examples
Complete CRUD Request Setup
Controller Usage
Route Definition
Troubleshooting
”Field is required” on partial updates
If you’re getting validation errors for fields not included in the request:- Ensure your request is using PATCH or PUT method (not POST)
- Verify the field isn’t marked as nullable when it shouldn’t be
- Check that you’re extending
CoreFormRequestand implementingrules()(notrules())
Rules not being transformed
Ifsometimes isn’t being added:
- Verify the request method is PATCH or PUT
- Check if the field is marked as
nullable(nullable fields don’t getsometimes) - Ensure you haven’t overridden
rules()method directly
Duplicate sometimes rules
This shouldn’t happen as the class checks for existing sometimes, but if it does:
- Remove manual
sometimesfrom yourrules()- let CoreFormRequest handle it - Check if you’ve overridden
prependSometimesToNonNullableRules()incorrectly
Related Documentation
- Core Form Components - Frontend form components
- Flash Messages - Success/error message handling
- Inertia.js Forms - Underlying form system