{"id":13828439,"url":"https://github.com/spatie/laravel-validation-rules","last_synced_at":"2025-05-13T22:12:32.348Z","repository":{"id":33126362,"uuid":"152587206","full_name":"spatie/laravel-validation-rules","owner":"spatie","description":"A set of useful Laravel validation rules","archived":false,"fork":false,"pushed_at":"2025-02-25T18:36:35.000Z","size":223,"stargazers_count":792,"open_issues_count":0,"forks_count":45,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-04-30T12:13:52.168Z","etag":null,"topics":["laravel","php","rules","validation"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie"}},"created_at":"2018-10-11T12:22:05.000Z","updated_at":"2025-04-20T10:17:25.000Z","dependencies_parsed_at":"2024-08-04T09:08:31.252Z","dependency_job_id":"59f00b30-8a1c-48a1-9b77-c6acf672fe00","html_url":"https://github.com/spatie/laravel-validation-rules","commit_stats":{"total_commits":181,"total_committers":26,"mean_commits":6.961538461538462,"dds":0.4033149171270718,"last_synced_commit":"d7d724fb4ed8a3ac2c3ddb3b8a44956e24b06415"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-validation-rules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-validation-rules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-validation-rules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-validation-rules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-validation-rules/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254036843,"owners_count":22003654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","php","rules","validation"],"created_at":"2024-08-04T09:02:46.941Z","updated_at":"2025-05-13T22:12:27.339Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie","https://www.patreon.com/spatie"],"categories":["PHP"],"sub_categories":[],"readme":"# A set of useful Laravel validation rules\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-validation-rules.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-validation-rules)\n[![Tests](https://img.shields.io/github/actions/workflow/status/spatie/laravel-validation-rules/run-tests.yml?branch=main\u0026label=Tests)](https://github.com/spatie/laravel-validation-rules/actions/workflows/run-tests.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-validation-rules.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-validation-rules)\n\nThis repository contains some useful Laravel validation rules.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-validation-rules.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-validation-rules)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/laravel-validation-rules\n```\n\nThe package will automatically register itself.\n\n### Translations\n\nIf you wish to edit the package translations, you can run the following command to publish them into your `resources/lang` folder\n\n```bash\nphp artisan vendor:publish --provider=\"Spatie\\ValidationRules\\ValidationRulesServiceProvider\"\n```\n\n## Available rules\n\n- [`Authorized`](#authorized)\n- [`CountryCode`](#countrycode)\n- [`Currency`](#currency)\n- [`Enum`](#enum)\n- [`ModelsExist`](#modelsexist)\n- [`Delimited`](#delimited)\n\n### `Authorized`\n\nDetermine if the user is authorized to perform an ability on an instance of the given model. The id of the model is the field under validation \n\nConsider the following policy:\n\n```php\nclass ModelPolicy\n{\n    use HandlesAuthorization;\n\n    public function edit(User $user, Model $model): bool\n    {\n        return $model-\u003euser-\u003eid === $user-\u003eid;\n    }\n}\n```\n\nThis validation rule will pass if the id of the logged in user matches the `user_id` on `TestModel` who's it is in the `model_id` key of the request.\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\Authorized;\n\npublic function rules()\n{\n    return [\n        'model_id' =\u003e [new Authorized('edit', TestModel::class)],\n    ];\n}\n```\n\nOptionally, you can provide an authentication guard as the third parameter.\n\n```php\nnew Authorized('edit', TestModel::class, 'guard-name')\n```\n\n#### Model resolution\nIf you have implemented the `getRouteKeyName` method in your model, it will be used to resolve the model instance. For further information see [Customizing The Default Key Name](https://laravel.com/docs/7.x/routing)\n\n### `CountryCode`\n\nDetermine if the field under validation is a valid [2 letter ISO3166 country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Current_codes) (example of valid country codes: `GB`, `DK`, `NL`).\n\n**Note** that this rule requires the package [`league/iso3166`](https://github.com/thephpleague/iso3166) to be installed: `composer require league/iso3166`\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\CountryCode;\n\npublic function rules()\n{\n    return [\n        'country_code' =\u003e ['required', new CountryCode()],\n    ];\n}\n```\n\nIf you want to validate a nullable country code field, you can call the `nullable()` method on the `CountryCode` rule. This way `null` and `0` are also passing values:\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\CountryCode;\n\npublic function rules()\n{\n    return [\n        'country_code' =\u003e [(new CountryCode())-\u003enullable()],\n    ];\n}\n```\n\n### `Currency`\n\nDetermine if the field under validation is a valid [3 letter ISO4217 currency code](https://en.wikipedia.org/wiki/ISO_4217#Active_codes) (example of valid currencies: `EUR`, `USD`, `CAD`).\n\n**Note** that this rule require the package [`league/iso3166`](https://github.com/thephpleague/iso3166) to be installed: `composer require league/iso3166`\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\Currency;\n\npublic function rules()\n{\n    return [\n        'currency' =\u003e ['required', new Currency()], // Must be present and a valid currency\n    ];\n}\n```\n\nIf you want to validate a nullable currency field, simple do not let it be required as described in the [Laravel Docs for implicit validation rules](https://laravel.com/docs/master/validation#implicit-rules):\n\u003e ... when an attribute being validated is not present or contains an empty string, normal validation rules, including custom rules, are not run\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\Currency;\n\npublic function rules()\n{\n    return [\n        'currency' =\u003e [new Currency()], // This will pass for any valid currency, an empty value or null\n    ];\n}\n```\n\n### `Enum`\n\nThis rule will validate if the value under validation is part of the given enum class. We assume that the enum class has a static `toArray` method that returns all valid values. If you're looking for a good enum class, take a look at [spatie/enum](https://github.com/spatie/enum) or [myclabs/php-enum](https://github.com/myclabs/php-enum).\n\nConsider the following enum class:\n\n```php\nclass UserRole extends MyCLabs\\Enum\\Enum\n{\n    const ADMIN = 'admin';\n    const REVIEWER = 'reviewer';\n}\n```\n\nThe `Enum` rule can be used like this:\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\Enum;\n\npublic function rules()\n{\n    return [\n        'role' =\u003e [new Enum(UserRole::class)],\n    ];\n}\n```\n\nThe request will only be valid if `role` contains `ADMIN` or `REVIEWER`.\n\n### `ModelsExist`\n\nDetermine if all of the values in the input array exist as attributes for the given model class. \n\nBy default the rule assumes that you want to validate using `id` attribute. In the example below the validation will pass if all `model_ids` exist for the `Model`.\n\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\ModelsExist;\n\npublic function rules()\n{\n    return [\n        'model_ids' =\u003e ['array', new ModelsExist(Model::class)],\n    ];\n}\n```\n\n\nYou can also pass an attribute name as the second argument. In the example below the validation will pass if there are users for each email given in the `user_emails` of the request.\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\ModelsExist;\n\npublic function rules()\n{\n    return [\n        'user_emails' =\u003e ['array', new ModelsExist(User::class, 'emails')],\n    ];\n}\n```\n\n### `Delimited`\n\nThis rule can validate a string containing delimited values. The constructor accepts a rule that is used to validate all separate values.\n\nHere's an example where we are going to validate a string containing comma separated email addresses.\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\Delimited;\n\npublic function rules()\n{\n    return [\n        'emails' =\u003e [new Delimited('email')],\n    ];\n}\n```\n\nHere's some example input that passes this rule:\n\n- `'sebastian@example.com, alex@example.com'`\n- `''`\n- `'sebastian@example.com'`\n- `'sebastian@example.com, alex@example.com, brent@example.com'`\n- `' sebastian@example.com   , alex@example.com  ,   brent@example.com  '`\n\nThis input will not pass:\n- `'@example.com'`\n- `'nocomma@example.com nocommatoo@example.com'`\n- `'valid@example.com, invalid@'`\n\n#### Setting a minimum\nYou can set minimum amout of items that should be present:\n\n```php\n(new Delimited('email'))-\u003emin(2)\n```\n\n- `'sebastian@example.com, alex@example.com'` // passes\n- `'sebastian@example.com'` // fails\n\n#### Setting a maximum\n\n```php\n(new Delimited('email'))-\u003emax(2)\n```\n\n- `'sebastian@example.com'` // passes\n- `'sebastian@example.com, alex@example.com, brent@example.com'` // fails\n\n#### Allowing duplicate items\n\nBy default the rule will fail if there are duplicate items found.\n\n- `'sebastian@example.com, sebastian@example.com'` // fails\n\nYou can allowing duplicate items like this:\n\n```php\n(new Delimited('numeric'))-\u003eallowDuplicates()\n```\n\nNow this will pass: `1,1,2,2,3,3`\n\n#### Customizing the separator\n\n```php\n(new Delimited('email'))-\u003eseparatedBy(';')\n```\n\n- `'sebastian@example.com; alex@example.com; brent@example.com'` // passes\n- `'sebastian@example.com, alex@example.com, brent@example.com'` // fails\n\n#### Skip trimming of items\n\n```php\n(new Delimited('email'))-\u003edoNotTrimItems()\n```\n\n- `'sebastian@example.com,freek@example.com'` // passes\n- `'sebastian@example.com, freek@example.com'` // fails\n- `'sebastian@example.com , freek@example.com'` // fails\n\n#### Composite rules\n\nThe constructor of the validator accepts a validation rule string, a validate instance, or an array.\n\n```php\nnew Delimited('email|max:20')\n```\n- `'short@example.com'` // passes\n- `'invalid'` // fails\n- `'loooooooonnnggg@example.com'` // fails\n\n#### Passing custom error messages\n\nThe constructor of the validator accepts a custom error messages array as second parameter.\n\n```php\n// in a `FormRequest`\n\nuse Spatie\\ValidationRules\\Rules\\Delimited;\n\npublic function rules()\n{\n    return [\n        'emails' =\u003e [new Delimited('email', $this-\u003emessages())],\n    ];\n}\n\npublic function messages()\n{\n    return [\n        'emails.email' =\u003e 'Not all the given e-mails are valid.',\n    ];\n}\n```\n\n### Testing\n\n``` bash\ncomposer test\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n### Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\n## Support us\n\nSpatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).\n\nDoes your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). \nAll pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-validation-rules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-validation-rules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-validation-rules/lists"}