Laravel

Laravel Validation: Custom Rules

January 16, 2026 1 min read 18 views

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.');
        }
    },
],
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!