Laravel 5 IsoCodes Validation
, (*1)
A simple Laravel 5 wrapper for the IsoCodes Validation library from ronanguilloux., (*2)
Installation
Step 1: Install Through Composer
``` bash
composer require loro102/isocodesvalidation, (*3)
### Step 2: Add the Service Provider (not needed with v2.x because of auto discovery)
Add the service provider in `app/config/app.php`
```php
'provider' => [
...
loro102\IsoCodesValidation\IsoCodesValidationServiceProvider::class,
...
];
Usage
Simple examples
// Checking out your e-commerce shopping cart?
$payload = [
'creditcard' => '12345679123456'
];
$rules = [
'creditcard' => 'creditcard'
];
$validator = Validator::make($payload, $rules);
Examples with parameter
Some rules need a reference to be validated against (e.g. country
for zipcode
)., (*4)
Just pass the name of the field holding the reference to the rule., (*5)
// 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);
Validation error messages
Error messages can contain the name and value of the field and the value of the reference, (*6)
$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 for more examples and documentation., (*7)
Testing
Run the tests with:, (*8)
vendor/bin/phpunit
License
GNU General Public License v3.0 only. Please see License File for more information., (*9)