axlon/laravel-postal-code-validation
关于
Worldwide postal code validation for Laravel
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文[!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
探索相关项目
加载中…