GithubHelp home page GithubHelp logo

realrashid / laravel-postal-code-validation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from axlon/laravel-postal-code-validation

0.0 2.0 0.0 34 KB

Worldwide postal code validation for Laravel

License: MIT License

PHP 100.00%

laravel-postal-code-validation's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.