Laravel Validation — Rules & Messages
Laravel's validation system validates incoming request data against a set of rules. If validation fails, the user is redirected back with error messages and old input automatically — no manual error handling code needed. Laravel provides 60+ built-in validation rules covering everything from basic required/max to complex conditional rules and custom closures.
Validation Examples
// In controller
$validated = $request->validate([
'title' => 'required|string|max:255|unique:posts,title',
'body' => 'required|string|min:10',
'email' => 'required|email|unique:users',
'category_id' => 'required|exists:categories,id',
'image' => 'nullable|image|mimes:jpg,png,webp|max:2048',
'tags' => 'array|min:1|max:5',
'tags.*' => 'exists:tags,id',
'published_at' => 'nullable|date|after:today',
]);
// Display errors in Blade
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{{-- Error for specific field --}}
<input name="title" class="@error('title') is-invalid @enderror">
@error('title')
<span class="error">{{ $message }}</span>
@enderrorEssential Validation Rules Reference
- required — field must be present and not empty. required_if:other_field,value — required only when another field equals a value
- string|max:255 — must be a string, max 255 characters. min:10 — at least 10 characters. between:5,500 — between 5 and 500
- unique:table,column — value must not exist in the table. For updates: unique:posts,email,{$post->id} to ignore the current record
- exists:table,column — value must exist in the table. Use for foreign keys: 'category_id' => 'required|exists:categories,id'
- image|mimes:jpg,png|max:2048 — must be an image file, specific MIME types only, max 2MB. Use for file uploads
- array|min:1|max:5 — must be an array with between 1 and 5 elements. tags.* applies rules to each array element
- nullable — field can be null/empty. Without this, an optional field that is empty will fail 'string' validation
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 Laravel Validation Rules Messages in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Use Form Request classes for reusable, testable validation.
Practice Task
Note
Practice Task - (1) Write a working example of Laravel Validation Rules Messages 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 Laravel Validation Rules Messages 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
- Laravel's validation system validates incoming request data against a set of rules.
- required — field must be present and not empty. required_if:other_field,value — required only when another field equals a value
- string|max:255 — must be a string, max 255 characters. min:10 — at least 10 characters. between:5,500 — between 5 and 500
- unique:table,column — value must not exist in the table. For updates: unique:posts,email,{$post->id} to ignore the current record