axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel

v2.0.1 2019-09-07 19:26 UTC

This package is auto-updated.

Last update: 2020-01-10 05:10:44 UTC


README

Worldwide postal code validation for Laravel, based on Google's Address Data Service.

68747470733a2f2f7472617669732d63692e6f72672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2e7376673f6272616e63683d6d6173746572 68747470733a2f2f706f7365722e707567782e6f72672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2f646f776e6c6f616473 68747470733a2f2f706f7365722e707567782e6f72672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2f76657273696f6e 68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572 68747470733a2f2f706f7365722e707567782e6f72672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2f6c6963656e7365

Requirements

This package has the following requirements:

  • PHP 7.1 or higher
  • Laravel (or Lumen) 5.1 or higher

Installation

You can install this package with Composer, by running the command below:

composer require axlon/laravel-postal-code-validation

Laravel 5.5+

If you use Laravel 5.5 or higher, that's it, continue to the usage section.

Laravel 5.1-5.4

If you're using an older version of Laravel, register the package's service provider to your application. You can do this by adding the following line to your config/app.php file:

'providers' => [
   ...
   Axlon\PostalCodeValidation\ValidationServiceProvider::class,
   ...
],

Lumen

If you are using Lumen, register the package by adding the following line to your bootstrap/app.php file:

$app->register(Axlon\PostalCodeValidation\ValidationServiceProvider::class);

Usage

Postal code validation perfectly integrates into your Laravel application, you can use it just like you would any framework validation rule.

Available rules

This package adds the following validation rules:

postal_code:foo,bar,...

The field under validation must be a valid postal code in at least one of the given countries. Arguments must be countries in ISO 3166-1 alpha-2 format.

'postal_code' => 'postal_code:NL,DE,FR,BE'

postal_code_for:foo,bar,...

The field under validation must be a postal code in at least one of the countries in the given fields only if at least one of the specified fields is present.

'billing.country' => 'required|string|max:2',
...
'shipping.country' => 'nullable|string|max:2',
'shipping.postal_code' => 'postal_code_for:billing.country,shipping.country'

Important: while this rule supports array references (e.g. postal_code_for:deliveries.*.country), this will not work in Laravel 5.1-5.3 due to framework limitations.

Fluent API

If you prefer using a fluent object style over string based rules, that's also available:

'postal_code' => [
    PostalCode::forCountry('NL')->or('BE'),
],

The same goes for the postal_code_for rule:

'billing.country' => 'required|string|max:2',
...
'shipping.country' => 'nullable|string|max:2',
'shipping.postal_code' => [
    PostalCode::forInput('billing.country')->or('shipping.country')
],

Adding an error message

To add a meaningful error message, add the following lines to resources/lang/{your language}/validation.php:

'postal_code' => 'Your message here',
'postal_code_for' => 'Your message here',

The following placeholders will be automatically filled for you:

Placeholder Description
:attribute The name of the field that was under validation
:countries The countries that were validated against (e.g. NL, BE)*
:examples Examples of allowed postal codes (e.g. 1234 AB, 4000)*

*The :countries and :examples placeholders may be empty if no valid countries are passed.

Manually validating

If you want to validate postal codes manually outside of Laravel's validation system, you can call the validator directly, like so:

$validator = app('\Axlon\PostalCodeValidation\Validator');
$validator->validate($countryCode, $postalCode); // returns a boolean

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

This open-source software is licenced under the MIT license. This software contains code generated from Google's Address Data Service, more information on this service can be found here.