Laravel custom validation rules

Carlos Mendez Jan 2026
3 tabs
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class Uppercase implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if ($value !== strtoupper($value)) {
            $fail("The {$attribute} must be uppercase.");
        }
    }
}
3 files · php Explain with highlit

Custom validation rules encapsulate complex validation logic in reusable classes. I create rule classes implementing ValidationRule with a validate() method receiving the attribute, value, and fail closure. Rules access databases, call APIs, or perform intricate checks. The Rule::unique()->ignore() pattern excludes current models during updates. Closure-based validation provides inline custom rules. The sometimes() method conditionally validates. Nested array validation uses dot notation. Custom error messages use translation keys. I combine rules with | or arrays. The validation system is extensible—perfect for business-specific constraints like checking inventory, validating coupons, or verifying external data.