Coder Social home page Coder Social logo

elcapo / laravel-isocodes-validation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pixelpeter/laravel-isocodes-validation

0.0 0.0 0.0 21 KB

Laravel 8+ wrapper for the IsoCodes Validation library from ronanguilloux

License: Other

PHP 100.00%

laravel-isocodes-validation's Introduction

Laravel 8+ IsoCodes Validation

Latest Version on Packagist Total Downloads Software License Travis Build Scrutinizer Code Quality Build Status Code Intelligence Status Coverage Status

A simple Laravel 8+ wrapper for the IsoCodes Validation library from ronanguilloux.

Installation

Step 1: Install Through Composer

composer require pixelpeter/laravel-isocodes-validation

Step 2: Add the Service Provider

(not needed starting with v2.x because of auto discovery)

Add the service provider in app/config/app.php

'provider' => [
    ...
    Pixelpeter\IsoCodesValidation\IsoCodesValidationServiceProvider::class,
    ...
];

Usage

Simple examples

// Checking out your e-commerce shopping cart?
$payload = [
    'creditcard' => '12345679123456'
];
$rules = [
    'creditcard' => 'creditcard'
];

$validator = Validator::make($payload, $rules);

Examples with reference parameter

Some rules need a reference to be validated against (e.g. country for zipcode).

Just pass the name of the field holding the reference to the rule.

// Sending letters to the Labrador Islands ?
$payload = [
    'zipcode' => 'A0A 1A0',
    'country' => 'CA'
];
$rules = [
    'zipcode' => 'zipcode:country'
];

$validator = Validator::make($payload, $rules);

// Publishing books?
$payload = [
    'isbn' => '2-2110-4199-X',
    'isbntype' => 13
];
$rules = [
    'zipcode' => 'isbn:isbntype'
];

$validator = Validator::make($payload, $rules);

Example with arrays and dot notation

(added in v3.x)

As suggested by @otr-tomek I've added support for all validation methods using arrays in dot notation as an input.

$payload = [
    'data' => [
        [
            'country' => 'DE',
            'zipcode' => 63741
        ],
        [
            'country' => 'AT',
            'zipcode' => 1180
        ]
  ] 
];

$validator = Validator::make($payload, [
    'data.*.zipcode' => 'zipcode:data.*.country'
]);

Validation error messages

Error messages can contain the name and value of the field and the value of the reference

$payload = [
    'phonenumber' => 'invalid',
    'country' => 'GB'
];
$rules = [
    'phonenumber' => 'phonenumber:country'
];

$validator = Validator::make($payload, $rules);

print $validator->errors()->first(); // The value "invalid" of phonenumber is not valid for "GB".

More Examples

Refer to IsoCodes Validation library for more examples and documentation.

Testing

Run the tests with:

vendor/bin/phpunit

License

GNU General Public License v3.0 only. Please see License File for more information.

laravel-isocodes-validation's People

Contributors

pixelpeter avatar masnathan avatar elcapo avatar

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.