Laravel Validation: Custom Rules
January 16, 2026
•
1 min read
•
18 views
Table of Contents
Custom validation rules:
Create Rule
php artisan make:rule Uppercase
Rule Class
class Uppercase implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (strtoupper($value) !== $value) {
$fail('The :attribute must be uppercase.');
}
}
}
Use Rule
$request->validate([
'name' => ['required', new Uppercase],
]);
Closure Rule
'email' => [
function ($attribute, $value, $fail) {
if (!str_ends_with($value, '@company.com')) {
$fail('Must be a company email.');
}
},
],
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!