mcpField
L
laravel:form-requests

laravel:form-requests

github.com/jpcaparasDevelopment157146 installsUpdated 17w ago
Repository

About laravel:form-requests

A Laravel guide to form requests and validation, aimed at developers moving validation and authorization out of controllers. It covers nested data rules, strict input checks, and testing.

Key features

  1. 01Centralize validation in Form Requests
  2. 02Control access with authorize
  3. 03Validate nested array fields
  4. 04Customize rules and error messages
  5. 05Reject undeclared input fields

Use cases

  1. 01Move controller validation into dedicated request classes
  2. 02Validate nested form data
  3. 03Catch extra input fields in development and CI

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/jpcaparas/superpowers-laravel --skill laravel:form-requests

After installing, type/laravel:form-requestsin Claude Code to confirm it loaded.

Details

From the project README

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

  • Define authorize() to gate access; prefer Policies for complex checks
  • Use rule objects: Rule::unique('users', 'email')->ignore($user->id)
  • Validate nested arrays: items.*.sku, addresses.home.city
  • Prefer nullable + specific rules instead of sometimes for optional fields
  • Standardize attribute names / messages via attributes() and messages()
  • Centralize common rules in custom Rule classes or traits
  • Return $request->safe()->only([...]) when partial updates are intended

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) => /* ... */);
  • Enable strict mode in dev/CI to catch typos and payload drift early; keep production lenient unless the API contract is strict

Testing

  • Feature test the endpoint: assert validation errors and success flows
  • Unit test custom validators and rule objects in isolation

Comments

Markdown supported