Form requests bundle authorization, rules, and custom error messages for a single HTTP action. Moving validation out of controllers keeps endpoints thin and makes reuse (API + web) easier.
When to reach for a form request
Use one when an endpoint has enough rules that the controller would become a wall of validate() arrays, or when you need a dedicated authorize() gate (e.g. only the owner may update).
1. Create the class
php artisan make:request StorePostRequest
2. Authorize the action
Return true for public forms, or delegate to gates/policies.
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorePostRequest extends FormRequest
{
public function authorize(): bool
{
return $this->user()?->can('create', Post::class) ?? false;
}
public function rules(): array
{
return [
'title' => ['required', 'string', 'max:200'],
'body' => ['required', 'string', 'max:50000'],
];
}
}
3. Custom messages and attributes
Optional messages() and attributes() keep copy out of the rules array.
public function messages(): array
{
return [
'title.required' => 'Give this post a short title.',
'body.required' => 'The body cannot be empty.',
];
}
public function attributes(): array
{
return [
'body' => 'post content',
];
}
4. Use it in the controller
Laravel injects the validated payload; use validated() only what you allow.
public function store(StorePostRequest $request)
{
$data = $request->validated();
$post = Post::create([
'title' => $data['title'],
'body' => $data['body'],
'user_id' => $request->user()->id,
]);
return redirect()->route('posts.show', $post);
}
5. Prepared / normalized input
Override prepareForValidation() to merge defaults or normalize strings before rules run.
protected function prepareForValidation(): void
{
$this->merge([
'title' => trim((string) $this->input('title', '')),
]);
}
Takeaway
One request class per “shape” of input keeps controllers readable and puts who may submit next to what is valid—especially helpful as your API surface grows.