https://github.com/pixelpeter/laravel-isocodes-validation
Laravel 8+ wrapper for the IsoCodes Validation library from ronanguilloux
https://github.com/pixelpeter/laravel-isocodes-validation
Last synced: 10 months ago
JSON representation
Laravel 8+ wrapper for the IsoCodes Validation library from ronanguilloux
- Host: GitHub
- URL: https://github.com/pixelpeter/laravel-isocodes-validation
- Owner: pixelpeter
- License: other
- Created: 2021-10-09T11:26:16.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-04-02T10:20:57.000Z (about 1 year ago)
- Last Synced: 2025-05-08T16:56:50.069Z (about 1 year ago)
- Language: PHP
- Size: 40 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel 8+ IsoCodes Validation
[](https://packagist.org/packages/pixelpeter/laravel-isocodes-validation)
[](https://packagist.org/packages/pixelpeter/laravel-isocodes-validation)
[](LICENSE.md)
[](https://coveralls.io/github/pixelpeter/laravel-isocodes-validation?branch=master)
[](https://github.com/pixelpeter/laravel-isocodes-validation/actions/workflows/run-tests.yml)
[](https://github.com/pixelpeter/laravel-isocodes-validation/actions/workflows/fix-php-code-style-issues.yml)
[](https://github.com/pixelpeter/laravel-isocodes-validation/actions/workflows/phpstan.yml)
[](https://github.com/pixelpeter/laravel-isocodes-validation/actions/workflows/dependabot-auto-merge.yml)
A simple Laravel 8+ wrapper for the [IsoCodes Validation library](https://github.com/ronanguilloux/IsoCodes) from
ronanguilloux.
## Installation
### Step 1: Install Through Composer
``` bash
composer require pixelpeter/laravel-isocodes-validation
```
### Step 2: Add the Service Provider
*(not needed starting with v2.x because of auto discovery)*
Add the service provider in `app/config/app.php`
```php
'provider' => [
...
Pixelpeter\IsoCodesValidation\IsoCodesValidationServiceProvider::class,
...
];
```
## Usage
### Simple examples
```php
// Checking out your e-commerce shopping cart?
$payload = [
'creditcard' => '12345679123456'
];
$rules = [
'creditcard' => 'creditcard'
];
$validator = Validator::make($payload, $rules);
```
### Examples with reference parameter
Some rules need a reference to be validated against (e.g. `country` for `zipcode`).
Just pass the name of the field holding the reference to the rule.
```php
// Sending letters to the Labrador Islands ?
$payload = [
'zipcode' => 'A0A 1A0',
'country' => 'CA'
];
$rules = [
'zipcode' => 'zipcode:country'
];
$validator = Validator::make($payload, $rules);
// Publishing books?
$payload = [
'isbn' => '2-2110-4199-X',
'isbntype' => 13
];
$rules = [
'zipcode' => 'isbn:isbntype'
];
$validator = Validator::make($payload, $rules);
```
### Example with arrays and dot notation
*(added in v3.x)*
As suggested by @otr-tomek I've added support for all validation methods using arrays in dot notation as an input.
```php
$payload = [
'data' => [
[
'country' => 'DE',
'zipcode' => 63741
],
[
'country' => 'AT',
'zipcode' => 1180
]
]
];
$validator = Validator::make($payload, [
'data.*.zipcode' => 'zipcode:data.*.country'
]);
```
### Validation error messages
Error messages can contain the name and value of the field and the value of the reference
```php
$payload = [
'phonenumber' => 'invalid',
'country' => 'GB'
];
$rules = [
'phonenumber' => 'phonenumber:country'
];
$validator = Validator::make($payload, $rules);
print $validator->errors()->first(); // The value "invalid" of phonenumber is not valid for "GB".
```
### More Examples
Refer to [IsoCodes Validation library](https://github.com/ronanguilloux/IsoCodes) for more examples and documentation.
## Testing
Run the tests with:
```bash
vendor/bin/phpunit
```
## License
GNU General Public License v3.0 only. Please see [License File](LICENSE.md) for more information.