axlon / laravel-postal-code-validation
Laravel和Lumen的全球邮编验证
v3.7.0
2024-09-10 15:23 UTC
Requires
- php: ^7.2 || ^8.0
- illuminate/contracts: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/validation: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.8
- phpunit/phpunit: ^8.5 || ^9.6 || ^10.5
- symfony/var-exporter: ^4.0 || ^5.0 || ^6.0 || ^7.0
README
基于谷歌地址数据服务的全球邮编验证,适用于Laravel。
要求
本软件包有以下要求
- PHP 7.2或更高版本
- Laravel(或Lumen)5.5或更高版本
安装
您可以使用Composer安装此软件包,运行以下命令
composer require axlon/laravel-postal-code-validation
如果您已启用包发现,那就完成了,继续到使用方法部分。如果您想手动注册包,可以将以下行添加到您的config/app.php
文件中
'providers' => [ ... Axlon\PostalCodeValidation\ValidationServiceProvider::class, ... ],
Lumen
如果您正在使用Lumen,可以通过向您的bootstrap/app.php
文件中添加以下行来注册该包
$app->register(Axlon\PostalCodeValidation\ValidationServiceProvider::class);
使用方法
邮政编码验证可以完美集成到您的Laravel应用程序中,您可以使用它就像使用任何框架验证规则一样。
可用规则
此软件包添加以下验证规则
postal_code:foo,bar,...
正在验证的字段必须在给定国家中的有效邮编之一。参数必须是ISO 3166-1 alpha-2格式的国家。
'postal_code' => 'postal_code:NL,DE,FR,BE'
postal_code_with:foo,bar,...
正在验证的字段必须在给定字段中的至少一个国家的邮编之一,前提是至少有一个指定的字段存在。
'billing.country' => 'required|string|max:2', ... 'shipping.country' => 'nullable|string|max:2', 'shipping.postal_code' => 'postal_code_with:billing.country,shipping.country'
流畅API
如果您更喜欢使用基于对象的流畅风格而不是基于字符串的规则,这也是可用的
'postal_code' => [ PostalCode::for('NL')->or('BE'), ],
对于postal_code_with
规则也是一样
'billing.country' => 'required|string|max:2', ... 'shipping.country' => 'nullable|string|max:2', 'shipping.postal_code' => [ PostalCode::with('billing.country')->or('shipping.country') ],
添加错误信息
要添加有意义的错误信息,将以下行添加到resources/lang/{your language}/validation.php
'postal_code' => 'Your message here', 'postal_code_with' => 'Your message here',
以下占位符将自动填充
如果未传递有效国家,则:countries
和:examples
占位符可能为空。
手动验证
如果您想在Laravel的验证系统之外手动验证邮编,可以直接调用验证器,如下所示
PostalCodes::passes($country, $postalCode); // returns a boolean
覆盖规则
根据您的用例,您可能希望覆盖用于验证国家邮编的模式。您可以在应用程序的中心位置(例如服务提供者)中添加以下代码来完成此操作
PostalCodes::override('country', '/your pattern/'); // You can also pass overrides as an array PostalCodes::override([ 'country 1' => '/pattern 1/', 'country 2' => '/pattern 2/', ]);
重要:如果您认为此软件包中提供的模式之一存在错误,请创建问题。
更新日志
有关最近更改的更多信息,请参阅更新日志。
贡献
有关详细信息,请参阅贡献。