php
42 lines · 6 steps
Normalizing input in a Laravel FormRequest
A FormRequest that authorizes, cleans, and validates article input before it reaches the controller.
Explained by
highlit
1<?php
2
3namespace App\Http\Requests;
4
5use Illuminate\Foundation\Http\FormRequest;
6use Illuminate\Support\Str;
7use Illuminate\Validation\Rule;
8
9class StoreArticleRequest extends FormRequest
10{
11 public function authorize(): bool
12 {
13 return $this->user()->can('create', Article::class);
14 }
15
16 protected function prepareForValidation(): void
17 {
18 $this->merge([
19 'slug' => Str::slug($this->input('slug') ?: $this->input('title', '')),
20 'title' => trim($this->input('title', '')),
21 'tags' => collect(explode(',', (string) $this->input('tags')))
22 ->map(fn (string $tag) => Str::lower(trim($tag)))
23 ->filter()
24 ->unique()
25 ->values()
26 ->all(),
27 'published' => $this->boolean('published'),
28 ]);
29 }
30
31 public function rules(): array
32 {
33 return [
34 'title' => ['required', 'string', 'max:150'],
35 'slug' => ['required', 'string', 'max:170', Rule::unique('articles', 'slug')],
36 'body' => ['required', 'string', 'min:50'],
37 'tags' => ['array', 'max:8'],
38 'tags.*' => ['string', 'max:30'],
39 'published' => ['boolean'],
40 ];
41 }
42}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1prepareForValidation lets you sanitize and reshape input before rules run, keeping controllers clean.
- 2Chaining collection methods turns messy comma-separated input into a deduplicated, trimmed array in one expression.
- 3Centralizing auth, normalization, and rules in one FormRequest keeps request concerns out of the controller.
Related explainers
php
<?php namespace App\Database;
Building a fluent SQL query builder in PHP
fluent-interface
method-chaining
sql
Intermediate
10 steps
php
<?php namespace App\Http\Controllers;
Time-limited signed download links in Laravel
signed urls
authorization
file streaming
Intermediate
6 steps
python
import os import uuid from flask import Blueprint, current_app, jsonify, request
Handling secure file uploads in Flask
file-upload
validation
blueprints
Intermediate
8 steps
php
<?php namespace App\Http;
Building an onion middleware pipeline in PHP
middleware
closures
array-reduce
Advanced
9 steps
php
<?php namespace Database\Seeders;
Building blog test data with Laravel factories
seeding
factories
relationships
Intermediate
9 steps
php
<?php namespace App\Notifications;
How a queued weekly digest email works in Laravel
notifications
queues
email
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/normalizing-input-in-a-laravel-formrequest-explained-php-6f20/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.