Package Data | |
---|---|
Maintainer Username: | axlon |
Package Create Date: | 2018-12-06 |
Package Last Update: | 2024-09-10 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-23 03:11:57 |
Package Statistics | |
---|---|
Total Downloads: | 2,139,092 |
Monthly Downloads: | 57,340 |
Daily Downloads: | 380 |
Total Stars: | 372 |
Total Watchers: | 4 |
Total Forks: | 29 |
Total Open Issues: | 0 |
Worldwide postal code validation for Laravel
You can install this package with Composer, by running the command below:
composer require axlon/laravel-postal-code-validation
If you are running Laravel 5.5 or higher, package discovery will automatically register the package for you after running the Composer command.
If you are running a Laravel version lower than 5.5, register the package by adding the service provider to the
providers array in your config/app.php
file:
'providers' => [
...
Axlon\PostalCodeValidation\ValidationServiceProvider::class,
...
],
Version 1.3.0 dropped support for Laravel 5.0. If you want to use this package with Laravel 5.0, target the 1.2.x family.
If you are running Lumen, register the package by adding the following line to your bootstrap/app.php
file:
$app->register(Axlon\PostalCodeValidation\ValidationServiceProvider::class);
Postal code validation perfectly integrates into your Laravel application, you can use it just like you would any framework validation rule.
You can call the rule as part of your validation string, the rule expects at least one country code (ISO 3166-1 alpha 2) to validate against.
$this->validate($request, [
'postal_code' => 'postal_code:NL,BE',
]);
If you prefer a more object-like fluent style, that's available too:
$this->validate($request, [
'postal_code' => [
PostalCode::forCountry('NL')->andCountry('BE'),
],
]);
If you want to validate a postal code against a country code that's passed in the same request, that's also possible. Simply put the name of the request variable instead of a country code (dot notation is supported).
$this->validate($request, [
'delivery.country' => 'string|size:2|required',
'delivery.postal_code' => 'postal_code:delivery.country|required',
]);
To add an error message your users will be able to understand, open resources/lang/{your language}/validation.php
and
add the following line to it:
'postal_code' => 'The :attribute field must be a valid postal code.',
The following placeholders are available:
| placeholder | description
|--------------|-------------
| :attribute
| The name of the attribute that was validated (e.g. postal_code
)
| :countries
| The countries that were validated against, for example NL, BE
, note that this placeholder may contain user input if you use the 'country code from request' feature
| :formats
| The formats that were validated against, for example: #### NN, ####
, note that this placeholder may be empty if no valid countries are passed
Special thanks to sirprize, the author of the underlying postal code validation library.