axlon/laravel-postal-code-validation
[!WARNING] This documentation is for version 4.x, which is still in development. For production, use version 3.x and follow the 3.x documentation.
Laravel Postal Code Validation
Adds postal code validation to Laravel, based on Google's libaddressinput.
Requirements
This package has the following requirements:
- Laravel 12 or greater
- PHP 8.2 or greater
Installation
You can install this package with Composer, by running the command below:
composer require axlon/laravel-postal-code-validation
Laravel registers the service provider through package discovery. To register it manually, add it to your Laravel
application's bootstrap/providers.php file:
return [
// ...
Axlon\PostalCodeValidation\PostalCodeServiceProvider::class,
];
Usage
Validate a postal code
The field under validation must be a valid postal code in at least one of the specified areas. Arguments must be countries in ISO 3166-1 alpha-2 format:
use Axlon\PostalCodeValidation\Rules\PostalCode;
request()->validate([
'address.postal_code' => ['required', PostalCode::of(['NL', 'BE'])],
]);
Validate a postal code against a dynamic area
You may also validate a postal code against an area code that is passed in the request:
use Axlon\PostalCodeValidation\Rules\PostalCode;
request()->validate([
'address.country' => ['required', 'in:NL,BE'],
'address.postal_code' => ['required', PostalCode::of('address.country')],
]);
String notation
Use postal_code with one or more comma-separated parameters. Just like with the object rule, you can mix literal area
codes and input field references:
request()->validate([
- 'address.postal_code' => ['required', PostalCode::of(['NL', 'BE])],
+ 'address.postal_code' => 'required|postal_code:NL,BE',
]);
Adding an error message
To add a meaningful error message, add an error message to resources/lang/{your language}/validation.php:
'postal_code' => 'The :attribute must be a valid postal code for :areas.',
The following placeholders will be automatically filled for you:
| Placeholder | Description |
|---|---|
| :attribute | The name of the field that was under validation |
| :areas | The areas that were validated against (e.g. NL, BE) |
License
This software is licenced under the MIT license.
Attribution
This software contains data derived from Google's Address Validation Metadata. The original data is provided by Google and is licensed under the CC-BY 4.0 license.
Modifications
- Only data relevant to postal code validation is included
- Regular expressions found in the data are adjusted to be compatible with PHP's regex engine
- The resulting data is converted into PHP resource files
Related Packages
Constraint class for international zipcode validation
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v3.11.1 | ^7.2 || | ^5.5 || | MIT | |
| v3.11.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.10.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.9.1 | ^7.2 || | ^5.5 || | MIT | |
| v3.9.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.8.1 | ^7.2 || | ^5.5 || | MIT | |
| v3.8.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.7.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.6.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.5.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.4.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.3.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.2.1 | ^7.2 || | ^5.5 || | MIT | |
| v3.2.0 | ^7.2 || | ^5.5 || | MIT | |
| v3.1.3 | ^7.2 | ^5.5 || | MIT | |
| v3.1.2 | ^7.2 | ^5.5 || | MIT | |
| v3.1.1 | ^7.2 | ^5.5 || | MIT | |
| v3.1.0 | ^7.2 | ^5.5 || | MIT | |
| v3.0.0 | ^7.2 | ^5.5 || | MIT | |
| v2.1.1 | ^7.1 | ^5.1 || | MIT | |
| v2.1.0 | ^7.1 | ^5.1 || | MIT | |
| v2.0.2 | ^7.1 | ^5.1 || | MIT | |
| v2.0.1 | ^7.1 | ^5.1 || | MIT | |
| v2.0.0 | ^7.1 | >=5.1,<5.9 | MIT | |
| v1.4.1 | ^7.1 | >=5.1,<5.9 | MIT | |
| v1.4.0 | ^7.1 | >=5.1,<5.9 | MIT | |
| v1.3.1 | ^7.1 | >=5.1,<5.9 | MIT | |
| v1.3.0 | ^7.1.3 | >=5.1,<5.9 | MIT | |
| v1.2.1 | ^7.0 | >=5.0,<5.8 | MIT | |
| v1.2.0 | ^7.0 | >=5.0,<5.8 | MIT |
Showing the latest 30 of 33. See every release on Packagist