Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Validation Rules Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Chaining Rules: Combine Spatie rules with standard Laravel rules using arrays. For example, use required|array|new Delimited('email') to enforce presence and structure of a list.
  • Customizing Delimited Logic: Use fluent methods to tailor Delimited behavior: ->min(1)->max(5)->allowDuplicates()->separatedBy(',').
  • Policy-Based Authorization: Use 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 Validation: Leverage 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.
  • Nullable Handling: For optional country/currency fields, drop required and rely on rule behavior: ['currency' => new Currency()] passes for valid values or empty/null.

Gotchas and Tips

  • Missing Dependencies: CountryCode and Currency require league/iso3166. Run composer require league/iso3166 or expect silent failures if omitted (it’s a suggested dependency).
  • Delimited Quirks:
    • Whitespace trimming is on by default—use doNotTrimItems() only if you need exact spacing (rarely needed).
    • Duplicate detection is strict unless allowDuplicates() is explicitly used.
    • separatedBy() must match exactly (e.g., ',' vs ';').
  • ModelsExist Attribute Resolution: By default it validates 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).
  • Enum Array Requirement: The enum class must have a static toArray() method returning valid values—common enums like spatie/enum and myclabs/php-enum provide this, but verify custom enums do too.
  • Translation Publishing: Run php artisan vendor:publish --provider="Spatie\ValidationRules\ValidationRulesServiceProvider" to override default error messages (especially helpful for localized Delimited validation failures).
  • Laravel Versioning: This package keeps pace with LTS versions (v3.4+ supports Laravel 11–13), but always verify compatibility in the changelog if upgrading Laravel or PHP.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport