milwad / laravel-validate
The Laravel-Validate package enhances Laravel validation capabilities with custom rules and methods for simplified and efficient validation logic.
Requires
- php: ^8.2
- ext-bcmath: *
- ext-ctype: *
- doctrine/inflector: ^2.0
- laravel/framework: 11.*|12.*|^13.0
Requires (Dev)
- cijic/phpmorphy: ^0.3.6
- imanghafoori/php-imports-analyzer: 1.0.*
- orchestra/testbench: ^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.0|^11.0|^12.5.12
Suggests
- cijic/phpmorphy: Required to validate German words with the ValidSingular and ValidPlural rules (^0.3.6).
Provides
None
Conflicts
None
Replaces
None
README
Laravel Validate adds 45+ ready-to-use validation rules to Laravel: phone and landline numbers per country, IBAN, credit cards, crypto addresses, slugs, case styles, JWTs, ULIDs, strong passwords and more. Every rule ships with translated error messages in 25 languages.
use Milwad\LaravelValidate\Rules\ValidPhoneNumber; use Milwad\LaravelValidate\Rules\ValidStrongPassword; use Milwad\LaravelValidate\Utils\Country; $request->validate([ 'phone' => ['required', new ValidPhoneNumber(Country::GERMANY)], 'password' => ['required', new ValidStrongPassword()], ]);
Table of Contents
- Requirements
- Installation
- Usage
- Available Rules
- Configuration
- Localization
- Related Packages
- Contributing
- Security
- License
- Support the Project
Requirements
- PHP 8.2 or higher
- Laravel 11, 12 or 13
- PHP extensions:
bcmath,ctype
Older Laravel versions are supported by older releases:
| Version | L6 | L7 | L8 | L9 | L10 | L11 | L12 | L13 |
|---|---|---|---|---|---|---|---|---|
| 1.5 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
| 1.6 | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| 1.7 | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| 1.8 | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| 1.9 | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ |
| 2.0 | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ |
Installation
Install the package with Composer:
composer require milwad/laravel-validate
The service provider is registered automatically through Laravel package discovery.
Optionally, publish the config file:
php artisan vendor:publish --tag="laravel-validate-config"
Usage
Every rule is a plain Laravel rule object. Create it with new and add it to your rules array, in a controller, a Form Request or Validator::make():
use Milwad\LaravelValidate\Rules\ValidPhoneNumber; use Milwad\LaravelValidate\Rules\ValidSlug; class StorePostRequest extends FormRequest { public function rules(): array { return [ 'slug' => ['required', new ValidSlug()], 'phone' => ['nullable', new ValidPhoneNumber()], ]; } }
Some rules take arguments, for example a country code for phone and landline numbers or a locale for singular/plural words:
use Milwad\LaravelValidate\Rules\ValidLandlineNumber; use Milwad\LaravelValidate\Rules\ValidPlural; use Milwad\LaravelValidate\Utils\Country; return [ 'office_phone' => [new ValidLandlineNumber(Country::IRAN)], 'collection' => [new ValidPlural('en')], ];
Click any rule in the table below to see its full documentation and options.
Available Rules
| # | Rule name | Usage |
|---|---|---|
| 1 | ValidBase64 | Validate base64 for ex (bWlsd2Fk) |
| 2 | ValidBitcoinAddress | Validate bitcoin address for ex (1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY) |
| 3 | ValidCamelCase | Validate camel case for ex (milwadDev) |
| 4 | ValidCapitalCharWithNumber | Validate string for ex (MILWAD-84) |
| 5 | ValidCarNumber | Validate car number for ex (KA01AB1234) |
| 6 | ValidCartNumberIran | Validate card number for ex (1234123412341234) |
| 7 | ValidCountry | Validate country name for ex (United Arab Emirates) |
| 8 | ValidCountryCode | Validate ISO 3166-1 country code for ex (US or USA) |
| 9 | ValidCreditCard | Validate credit card for ex (4111111111111111) |
| 10 | ValidCssColor | Validate CSS color for ex (#fcba03, rgb(0 0 0 / 50%), oklch(0.7 0.15 180), red) |
| 11 | ValidDiscordUsername | Validate Discord username for ex (Milwad#2134) |
| 12 | ValidDomain | Validate domain name for ex (github.com) |
| 13 | ValidDuplicate | Validate duplicate string/number for ex (1123456) |
| 14 | ValidDuplicateCharacter | Validate duplicate characters for ex (1,2,3,4,5,6,7,8,9) |
| 15 | ValidEthereumAddress | Validate Ethereum address with EIP-55 checksum for ex (0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed) |
| 16 | ValidEvenNumber | Validate number is even for ex (1024) |
| 17 | ValidGithubUsername | Validate GitHub username for ex (milwad-dev) |
| 18 | ValidHashtag | Validate hashtag for ex (#milwad) |
| 19 | ValidHexColor | Validate hex color for ex (#fcba03) |
| 20 | ValidHtmlTag | Validate HTML tags for ex () |
| 21 | ValidIban | Validate IBAN for ex (IR062960000000100324200001) |
| 22 | ValidImei | Validate IMEI phone for ex (354809104295874) |
| 23 | ValidInstagramHandle | Validate Instagram handle for ex (milwad.dev) |
| 24 | ValidIpAddressIPV4 | Validate IPv4 address for ex (129.144.50.56) |
| 25 | ValidIpAddressIPV6 | Validate IPv6 address for ex (2001:db8:3333:4444:5555:6666:7777:8888) |
| 26 | ValidIranPostalCode | Validate Iran postal code for ex (3354355599) |
| 27 | ValidJalaliDate | Validate Jalali date for ex (1384/8/25) |
| 28 | ValidJwt | Validate JWT token |
| 29 | ValidKebabCase | Validate kebab case for ex (milwad-dev) |
| 30 | ValidLandlineNumber | Validate landline number |
| 31 | ValidLatitude | Validate latitude for ex (50.1109) |
| 32 | ValidLongitude | Validate longitude for ex (51.377225156511855) |
| 33 | ValidMongoObjectId | Validate MongoDB ObjectId for ex (507f1f77bcf86cd799439011) |
| 34 | ValidNameDaysWeek | Validate day of week (monday) |
| 35 | ValidNationalCard | Validate national card for ex (015016437) |
| 36 | ValidNoEmoji | Validate string without emoji for ex (Hello world) |
| 37 | ValidNoHtml | Validate string without HTML |
| 38 | ValidOddNumber | Validate number is odd for ex (4321) |
| 39 | ValidPascalCase | Validate PascalCase for ex (MilwadDev) |
| 40 | ValidPattern | Validate custom pattern |
| 41 | ValidPhoneNumber | Validate phone number for ex (09366000000) |
| 42 | ValidPlural | Validate plural word for ex (cameras) |
| 43 | ValidPort | Validate port value for ex (8080) |
| 44 | ValidSingular | Validate singular word for ex (camera) |
| 45 | ValidSlashEndOfString | Validate string ending with slash (milwad/) |
| 46 | ValidSlug | Validate slug for ex (milwad-dev) |
| 47 | ValidSnakeCase | Validate snake_case for ex (milwad_dev) |
| 48 | ValidStrongPassword | Validate password for ex (Milwad123!) |
| 49 | ValidUlid | Validate ULID |
| 50 | ValidUrl | Validate URL for ex (https://www.google.com) |
| 51 | ValidUsername | Validate username for ex (milwad) |
| 52 | ValidUuid | Validate UUID for ex (123e4567-e89b-12d3-a456-426655440000) |
| 53 | ValidVatId | Validate VAT ID (EU format) |
| 54 | ValidXHandle | Validate X (Twitter) handle for ex (milwad_dev) |
Configuration
After publishing, the config file lives at config/laravel-validate.php.
String-based rules (using_container)
By default, rules are used as objects. If you prefer Laravel's string syntax, enable using_container:
'using_container' => true,
Every rule is then registered under its class name, so you can write:
'phone_number' => 'required|ValidPhoneNumber',
Custom phone & landline validators
ValidPhoneNumber and ValidLandlineNumber accept a country code. The built-in codes are listed under phone-country and landline-country in the config file. You can add your own country, or replace an existing one.
1. Create a validator class that implements the matching interface:
namespace App\Validators; use Milwad\LaravelValidate\Utils\CountryPhoneValidator\CountryPhoneValidator; class XYPhoneValidator implements CountryPhoneValidator { public function validate($value): bool { return (bool) preg_match('/^\+1234\d{10}$/', $value); } }
For landline numbers, implement Milwad\LaravelValidate\Utils\CountryLandlineValidator\CountryLandlineValidator instead.
2. Register it in config/laravel-validate.php:
'phone-country' => [ // Existing validators... 'XY' => \App\Validators\XYPhoneValidator::class, ],
3. Use it:
return [ 'phone' => [new ValidPhoneNumber('XY')], ];
Singular & plural rules
ValidSingular and ValidPlural use the locale passed to the rule. If you pass none, they use grammatical-number-locale from the config, and if that is null, your app's app.locale.
Most languages are checked with doctrine/inflector. German needs an extra package:
composer require cijic/phpmorphy
To support another language, add a class that implements Milwad\LaravelValidate\Utils\GrammaticalNumber\GrammaticalNumberChecker to the grammatical-number config array.
Localization
Error messages are translated into these languages:
| Language | Code | Language | Code |
|---|---|---|---|
| Arabic | ar |
Japanese | ja |
| Azerbaijani | az |
Korean | ko |
| Bangla | bn |
Kurdish (Sorani) | ku_so |
| Catalan | ca |
Macedonian | mk |
| Chinese | zh_CN |
Persian | fa |
| Dutch | nl |
Portuguese (Brazil) | pt_BR |
| English | en |
Russian | ru |
| French | fr |
Sinhala | si |
| German | de |
Spanish | es |
| Greek | el |
Swedish | sv |
| Hindi | hi |
Turkish | tr |
| Indonesian | id |
Ukrainian | uk |
| Italian | It |
To customize the messages, publish the language file into your app's lang directory:
php artisan vendor:publish --tag="validate-lang-en"
Replace en with any code from the table above.
Is your language missing? Open an issue or send a pull request that adds it.
Related Packages
- Laravel Validate Filament: brings these rules to Filament forms as chainable field methods.
Contributing
Contributions are welcome: new rules, country validators, translations and bug fixes. Run the test suite before you open a pull request:
composer test
This project exists thanks to all the people who contribute.
Security
If you find a security issue, please email milwad.dev@gmail.com instead of using the issue tracker.
License
Laravel Validate is created by Milwad Khosravi and released under the MIT License.
Star History
Support the Project
If this package helps you, you can buy me a coffee ❤️
- Iranian gateway: https://daramet.com/milwad_khosravi
- MetaMask wallet:
0xf208a562c5a93DEf8450b656c3dbc1d0a53BDE58