Form Request Classes for Complex Validation
Form Requests move validation logic out of controllers into dedicated classes. They define both validation rules and authorization logic, keeping controllers clean. Generate with php artisan make:request StorePostRequest.
Form Request Class
// php artisan make:request StorePostRequest
class StorePostRequest extends FormRequest
{
// Authorization check
public function authorize(): bool
{
return auth()->check(); // Only authenticated users
}
// Validation rules
public function rules(): array
{
return [
'title' => 'required|string|max:255|unique:posts',
'body' => 'required|string|min:10',
'category_id' => 'required|exists:categories,id',
'image' => 'nullable|image|max:2048',
];
}
// Custom error messages
public function messages(): array
{
return [
'title.required' => 'Every post needs a title!',
'body.min' => 'Your post must be at least 10 characters.',
];
}
}
// Controller becomes clean:
public function store(StorePostRequest $request)
{
// Validation already passed!
$post = Post::create($request->validated());
return redirect()->route('posts.show', $post);
}Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and request timeline
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
- Document validation rules with comments explaining why - e.g., // max:5 because users complained about spam when unlimited
Tip
Tip
Practice Form Request Classes for Complex Validation in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
HTML5 validation = zero JavaScript - the browser handles it
Practice Task
Note
Practice Task - (1) Write a working example of Form Request Classes for Complex Validation from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Form Request Classes for Complex Validation is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Form Requests move validation logic out of controllers into dedicated classes.
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and request timeline