Files
SealShare/.claude/skills/laravel-best-practices/rules/validation.md
T
Andreas Reinhold / reiniandClaude Opus 4.8 4391ad7fb9 Regenerate Boost guidelines and skills
Byproduct of the Laravel 13 upgrade: composer's post-update-cmd runs
`artisan boost:update`, and Boost went 2.1.3 to 2.4.x with the update.

Refreshes CLAUDE.md and boost.json for the Laravel 13 / Livewire 4 stack,
updates the livewire, pest and tailwindcss skills, replaces the
developing-with-fortify skill with fortify-development, and adds new
laravel-best-practices and octane-development skills.

Generated output, not hand-edited.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-23 11:17:04 +02:00

1.7 KiB

Validation & Forms Best Practices

Use Form Request Classes

Extract validation from controllers into dedicated Form Request classes.

Incorrect:

public function store(Request $request)
{
    $request->validate([
        'title' => 'required|max:255',
        'body' => 'required',
    ]);
}

Correct:

public function store(StorePostRequest $request)
{
    Post::create($request->validated());
}

Array vs. String Notation for Rules

Array syntax is more readable and composes cleanly with Rule:: objects. Prefer it in new code, but check existing Form Requests first and match whatever notation the project already uses.

// Preferred for new code
'email' => ['required', 'email', Rule::unique('users')],

// Follow existing convention if the project uses string notation
'email' => 'required|email|unique:users',

Always Use validated()

Get only validated data. Never use $request->all() for mass operations.

Incorrect:

Post::create($request->all());

Correct:

Post::create($request->validated());

Use Rule::when() for Conditional Validation

'company_name' => [
    Rule::when($this->account_type === 'business', ['required', 'string', 'max:255']),
],

Use the after() Method for Custom Validation

Use after() instead of withValidator() for custom validation logic that depends on multiple fields.

public function after(): array
{
    return [
        function (Validator $validator) {
            if ($this->quantity > Product::find($this->product_id)?->stock) {
                $validator->errors()->add('quantity', 'Not enough stock.');
            }
        },
    ];
}