axlon / laravel-postal-code-validation by axlon

Worldwide postal code validation for Laravel and Lumen
1,686,105
364
4
Package Data
Maintainer Username: axlon
Package Create Date: 2018-12-06
Package Last Update: 2024-03-12
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-26 03:14:16
Package Statistics
Total Downloads: 1,686,105
Monthly Downloads: 55,266
Daily Downloads: 2,248
Total Stars: 364
Total Watchers: 4
Total Forks: 29
Total Open Issues: 0

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.