Skip to content

realrashid/laravel-postal-code-validation

 
 

Repository files navigation

laravel-postal-code-validation

Worldwide postal code validation for Laravel

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 are running Laravel 5.5 or higher, package discovery will automatically register the package for you after running the Composer command.

Laravel 5.4 and below

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,
   ...
],

A note on Laravel 5.0

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.

Lumen

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);

Usage

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

Using the rule as a string

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',
]);

Using the rule directly

If you prefer a more object-like fluent style, that's available too:

$this->validate($request, [
    'postal_code' => [
        PostalCode::forCountry('NL')->andCountry('BE'),
    ],
]);

Country code from request

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',
]);

Adding an error message

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

Special thanks to sirprize, the author of the underlying postal code validation library.

About

Worldwide postal code validation for Laravel

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%