38

GitHub - axlon/laravel-postal-code-validation: Worldwide postal code validation...

 5 years ago
source link: https://github.com/axlon/laravel-postal-code-validation
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

laravel-postal-code-validation

Worldwide postal code validation for Laravel

68747470733a2f2f7472617669732d63692e6f72672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2e7376673f6272616e63683d6d6173746572 68747470733a2f2f706f7365722e707567782e6f72672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2f646f776e6c6f616473 68747470733a2f2f706f7365722e707567782e6f72672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2f76657273696f6e 68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572 68747470733a2f2f706f7365722e707567782e6f72672f61786c6f6e2f6c61726176656c2d706f7374616c2d636f64652d76616c69646174696f6e2f6c6963656e7365

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

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

Special thanks

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK