Form requests and validation

From jpcaparas/superpowers-laravel · View interactive version

Form requests and validation is an AI agent skill from the GitHub repository jpcaparas/superpowers-laravel. Move validation and authorization into Form Requests; use rule objects, custom messages, and nested data handling to keep controllers slim

How to use this skill

Skill file

Form Requests and Validation

Promote validation and authorization to dedicated Form Request classes. Keep controllers focused on orchestration and domain intents.

Commands

# Create a request
sail artisan make:request UpdateProfileRequest      # or: php artisan make:request UpdateProfileRequest

# Use in controller method signature
public function update(UpdateProfileRequest $request) {
    $data = $request->validated();
    // ...
}

Patterns

Laravel 13+

// Strict mode: reject undeclared input fields
use Illuminate\Foundation\Http\Attributes\FailOnUnknownFields;

#[FailOnUnknownFields]
class StoreUserRequest extends FormRequest { /* ... */ }

// Or globally (AppServiceProvider) — strict in non-production
FormRequest::failOnUnknownFields(! app()->isProduction());

// Validate arbitrary JSON payloads against a schema
use Illuminate\JsonSchema\Types\Type;

$request->validate([
    'data' => [Rule::jsonSchema(Type::object([
        'name' => Type::string()->required(),
        'age'  => Type::integer()->min(0),
    ]))],
]);

// Typed enum handling for optional input
$request->whenFilledEnum('status', Status::class, fn (Status $status) => /* ... */);

Testing

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.