spatie/laravel-validation-rules
Handy Laravel validation rules by Spatie: Authorized (policy checks), CountryCode, Currency, Enum, ModelsExist, and Delimited. Installs via Composer, auto-registers, and supports publishing translations for customization.
Start by installing the package with composer require spatie/laravel-validation-rules. No service provider registration is needed—Laravel’s auto-discovery handles it. The first practical use case is often validating array inputs with ModelsExist or validating comma-separated values with Delimited. For example, to validate a list of user IDs passed in a request array, use:
use Spatie\ValidationRules\Rules\ModelsExist;
$request->validate([
'user_ids' => ['required', 'array', new ModelsExist(User::class)],
]);
Check the README’s “Available rules” section to quickly identify which rule fits your need—especially Delimited, Enum, CountryCode, and Currency, which are frequently useful in day-to-day form and API validation.
required|array|new Delimited('email') to enforce presence and structure of a list.Delimited behavior: ->min(1)->max(5)->allowDuplicates()->separatedBy(',').Authorized in FormRequests to enforce fine-grained permissions (e.g., new Authorized('update', Post::class)), ensuring users can only act on entities they own.Enum with your own or third-party enums (e.g., spatie/enum or myclabs/php-enum) to restrict inputs to known constants without repetitive manual checks.required and rely on rule behavior: ['currency' => new Currency()] passes for valid values or empty/null.CountryCode and Currency require league/iso3166. Run composer require league/iso3166 or expect silent failures if omitted (it’s a suggested dependency).doNotTrimItems() only if you need exact spacing (rarely needed).allowDuplicates() is explicitly used.separatedBy() must match exactly (e.g., ',' vs ';').id, but if using a different column (e.g., email), double-check the model’s column name matches and that your query isn’t ambiguous (i.e., new ModelsExist(User::class, 'email') requires a unique email column).toArray() method returning valid values—common enums like spatie/enum and myclabs/php-enum provide this, but verify custom enums do too.php artisan vendor:publish --provider="Spatie\ValidationRules\ValidationRulesServiceProvider" to override default error messages (especially helpful for localized Delimited validation failures).How can I help you explore Laravel packages today?