datalogix / laravel-validation
Laravel Validation is a package that brings the power of Respect Validation.
Requires
- php: ^8.2
- illuminate/support: ^12.0|^13.0
- illuminate/validation: ^12.0|^13.0
- respect/validation: ^2.5
Requires (Dev)
- larastan/larastan: ^3.13
- laravel/pint: ^1.30
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
Suggests
- ext-bcmath: Required to use the iban rule
- ext-fileinfo: Required to use the mimetype rule
- giggsey/libphonenumber-for-php-lite: Required to use the phone rule with a country code
Provides
None
Conflicts
None
Replaces
None
README
Laravel Validation is a package that brings the power of Respect Validation.
Installation
You can install the package via composer:
composer require datalogix/laravel-validation
The package will automatically register itself.
Requirements
- PHP 8.2+
- Laravel 12 or 13
Some rules need optional dependencies:
| Rule | Requires |
|---|---|
iban |
ext-bcmath |
mimetype |
ext-fileinfo |
phone with a country code (phone:BR) |
composer require giggsey/libphonenumber-for-php-lite |
Usage
Use any Respect Validation rule as a Laravel string rule:
$request->validate([ 'cpf' => ['required', 'cpf'], 'birthdate' => ['required', 'minAge:18'], 'video' => ['nullable', 'videoUrl:youtube'], 'tags' => ['array', 'subset:php,laravel'], ]);
Rule names
- Use the Respect rule name in camelCase:
cpf,videoUrl:youtube,minAge:18. - Rules that require other rules or objects (
each,not,allOf,keywith an inner rule, ...) are not available as string rules. notBlank,notEmptyandnotOptionalalso run on empty or missing values.
Note
Laravel's built-in rules (email, regex, json, min, max, in, exists, unique, file, image, lowercase, uppercase, ends_with, contains, base64 on Laravel 13, etc.) and rules registered with Validator::extend() always take precedence over Respect rules with the same name. Use fileExists for Respect's exists rule.
Laravel also turns int and bool into integer and boolean; use intVal and boolType for the Respect rules.
Warning
This package registers a custom validator resolver. If another package also calls Validator::resolver(), only the last one registered will be used.
Rule parameters
Parameters are passed to the rule constructor, in order, and converted to its types:
| Parameter type | Accepted values | Example |
|---|---|---|
| Boolean | true/false, 1/0, on/off, yes/no |
domain:false |
| Numeric | Numbers | length:1e1 is 10 |
| Nullable | An empty value or null (any case) skips the parameter |
length:,5 |
| Array | The remaining parameters become the array | containsAny:foo,bar, subset:a,b |
| Untyped | Any value, including an empty one | equals: compares with an empty value |
An array given through the array rule syntax is used as is ([['subset', ['a', 'b']]]).
These throw an InvalidArgumentException:
- Any other value for a boolean parameter, including an empty one.
- Invalid numbers and integers out of range (
length:abc,length:5.5). - A value of the wrong type (
key:foo,bar). - An empty value for any other typed parameter (
extension:,,countryCode:,). - An empty item in a list (
subset:a,). lengthwith both bounds empty (length:,).
Error messages
The package ships with error messages in English (en) and Brazilian Portuguese (pt_BR).
Overriding messages
Messages defined in your application's lang/{locale}/validation.php take precedence (except for uploaded, see below), so you can override them:
// lang/en/validation.php 'cpf' => 'The :attribute field is not a valid CPF.',
You can also publish the package translations to lang/vendor/laravel-validation:
php artisan vendor:publish --tag=laravel-validation-lang
Placeholders
The placeholders are replaced with the rule parameters:
:valueand:min: the first parameter:max: the second parameter (or the first, when there is only one):values: all parameters, separated by commas
Skipped parameters are left out of :values and replaced with an empty value in :value, :min and :max, and an array parameter shows only its values.
The length rule
The message depends on the bounds, unless you replace the length message:
| Bounds | Inclusive | Exclusive (length:10,20,false) |
|---|---|---|
| Both | length |
length_exclusive |
Minimum only (length:10) |
length_min |
length_min_exclusive |
Maximum only (length:,5) |
length_max |
length_max_exclusive |
None (length) |
length_unbounded |
— |
You can override these messages with the same keys in your lang/{locale}/validation.php.
The uploaded rule
The uploaded rule uses the package message instead of Laravel's "failed to upload" line unless you pass a custom message for it or define the line in a locale the package does not ship (it ships en and pt_BR).
The package cannot tell your line from one published by a translation package, so a published validation.uploaded in another locale keeps Laravel's "failed to upload" meaning; replace that line or pass a custom message.
Available rules
Every Respect Validation 2.x rule can be used, written in camelCase (e.g. VideoUrl → videoUrl). See also the Respect Validation repository.
Laravel Boost
The package ships an AI guideline for Laravel Boost. To install it, select datalogix/laravel-validation when php artisan boost:install asks for third-party guidelines.
With --no-interaction, Boost only includes the packages listed in boost.json, so add the package to its packages key first:
{
"packages": ["datalogix/laravel-validation"]
}
License
MIT License. See LICENSE.