wsu-das / laravel-isocodes-validation
Laravel wrapper for the IsoCodes Validation library from ronanguilloux
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: ^7.4|^8.0
- ronanguilloux/isocodes: ^2.3
Requires (Dev)
- laravel/laravel: ^7.0|^8.0
- mockery/mockery: ^1.4
- php-coveralls/php-coveralls: ^2.3
- phpunit/phpunit: ^9.3
README
A simple Laravel wrapper for the IsoCodes Validation library
Installation
composer require wsu-das/laravel-isocodes-validation
Usage
Simple examples
// 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.
// 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
As suggested by @otr-tomek I've added support for all validation methods using arrays in dot notation as an input.
$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
$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.
Testing
Run the tests with:
vendor/bin/phpunit
License
GNU General Public License v3.0 only. Please see License File for more information.