2017 © Pedro Peláez
 

library iso-codes-validator

Symfony validator wrapper of ronanguilloux/isocodes

image

sllh/iso-codes-validator

Symfony validator wrapper of ronanguilloux/isocodes

  • Tuesday, February 27, 2018
  • by Soullivaneuh
  • Repository
  • 3 Watchers
  • 19 Stars
  • 34,839 Installations
  • PHP
  • 2 Dependents
  • 1 Suggesters
  • 8 Forks
  • 31 Open issues
  • 26 Versions
  • 8 % Grown

The README.md

IsoCodesValidator

Symfony validator wrapper of IsoCodes project., (*1)

Latest Stable Version Latest Unstable Version License Dependency Status Reference Status, (*2)

Total Downloads Monthly Downloads Daily Downloads, (*3)

Build Status AppVeyor Status Scrutinizer Code Quality Code Climate Coverage Status SensioLabsInsight, (*4)

Documentation

All the installation and usage instructions are located in this README. Check it for specific version:, (*5)

  • 4.x(unstable) with support for Symfony ^2.7|^3.0 and Silex ^1.2|^2.0
  • 3.x with support for Symfony ^2.7|^3.0 and Silex ^1.2|^2.0
  • 2.x with support for Symfony ^2.7|^3.0 and Silex ^1.2|^2.0
  • 1.x with support for Symfony ^2.3|^3.0 and Silex ^1.1

Prerequisites

This version of the project requires:, (*6)

  • PHP 5.6+ or 7.0+
  • Symfony Validator component 2.7+
  • Symfony 2.7+ or 3.0+ for bundle integration
  • Silex 1.2+ for service provider integration

Installation

First of all, you need to require this library through composer:, (*7)

``` bash $ composer require sllh/iso-codes-validator, (*8)


After this, you can use it as is. If you are using it on a **Symfony** or **Silex** project, you should read the following instructions for a better integration. ### As a Symfony bundle If your project **is not using** [Symfony Full Stack](http://symfony.com/projects/symfonyfs), you must add the following dependencies: ```bash $ composer require symfony/dependency-injection symfony/http-kernel symfony/finder

Translations

If you wish to use default validator messages translations in this bundle, you have to make sure you have translator enabled in your config., (*9)

``` yaml, (*10)

app/config/config.yml

framework: translator: ~, (*11)


#### Enable the bundle ``` php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new SLLH\IsoCodesValidator\Bridge\Symfony\Bundle\SLLHIsoCodesValidatorBundle(), ); }

As a Silex service provider

Add Silex dependency on your project:, (*12)

$ composer require silex/silex

Register the service

Silex 1.x
use Silex\Provider\TranslationServiceProvider();
use SLLH\IsoCodesValidator\Bridge\Silex\IsoCodesValidatorSilex1ServiceProvider;

// Get translation working
$app->register(new TranslationServiceProvider());

// Register the provider
$app->register(new IsoCodesValidatorSilex1ServiceProvider());
Silex 2.x
use Silex\Provider\LocaleServiceProvider();
use Silex\Provider\TranslationServiceProvider();
use SLLH\IsoCodesValidator\Bridge\Silex\IsoCodesValidatorServiceProvider();

// Get translation working
$app->register(new LocaleServiceProvider());
$app->register(new TranslationServiceProvider(), array(
    'locale_fallbacks' => array('en'),
));

// Register the provider
$app->register(new IsoCodesValidatorServiceProvider());

Usage

IsoCodesValidator is based on Symfony Validator library., (*13)

Manual usage

Create and use IsoCodes constraints by using the Symfony Validation class:, (*14)

use Symfony\Component\Validator\Validation;
use SLLH\IsoCodesValidator\Constraints\Vat;

$validator = Validation::createValidator();

$violations = $validator->validateValue('DE123456789', new Vat());

With annotations

Validation of objects is possible using "constraint mapping". With such a mapping you can put constraints onto properties and objects of classes. Whenever an object of this class is validated, its properties and method results are matched against the constraints., (*15)

use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
use SLLH\IsoCodesValidator\Constraints as IsoCodesAssert;

class Company
{
    /**
     * @Assert\NotBlank
     * @IsoCodesAssert\Siret
     */
    private $siret;

    /**
     * @Assert\NotBlank
     * @IsoCodesAssert\Siren
     */
    private $siren;

    /**
     * @IsoCodesAssert\Vat
     */
    private $vat;

    /**
     * @IsoCodesAssert\ZipCode(country = "FR")
     */
    private $zipCode;

    public function __construct($siret, $siren, $vat, $zipCode)
    {
        $this->siret = $siret;
        $this->siren = $siren;
        $this->vat = $vat;
        $this->zipCode = $zipCode
    }
}

$validator = Validation::createValidatorBuilder()
    ->enableAnnotationMapping()
    ->getValidator();

$company = new Company('48853781200015', '432167567', 'DE123456789', '59000');

$violations = $validator->validate($company);

Silex method

You can validate values directly using the validateValue validator method, (*16)

use SLLH\IsoCodesValidator\Constraints as IsoCodesAssert;

$app->get('/validate/{vat}', function ($vat) use ($app) {
    $errors = $app['validator']->validateValue($vat, new IsoCodesAssert\Vat());

    if (count($errors) > 0) {
        return (string) $errors;
    } else {
        return 'The VAT is valid';
    }
});

More implementations could be found on official Silex Validator usage documentation., (*17)

Remember to replace Assert by IsoCodesAssert with the correct import statement., (*18)

Constraints reference

Constraints classes can be found on src/Constraints., (*19)

All works "as is" without any options unless message., (*20)

Only ZipCode constraint accept a country option to limit validation ('all' by default)., (*21)

Please note that some IsoCodes classes are already implemented on Symfony Validator component. It's up to you to decide which one to use., (*22)

If you think an IsoCodes class is missing, feel free to open an issue or make a PR., (*23)

License

This bundle is under the MIT license. See the complete license on the LICENSE file., (*24)

TODO

  • Try to implement OrganismeType12NormeB2 (Maybe with a special form type?)
  • Implement and test xml/yaml assert config for Symfony: http://symfony.com/doc/current/book/validation.html#the-basics-of-validation

The Versions

27/06 2015

v1.0.5

1.0.5.0

Symfony validator wrapper of ronanguilloux/isocodes

  Sources   Download

MIT

The Requires

 

The Development Requires

validator symfony iso codes

23/06 2015

v1.0.4

1.0.4.0

Symfony validator wrapper of ronanguilloux/isocodes

  Sources   Download

MIT

The Requires

 

The Development Requires

validator symfony iso codes

02/06 2015
01/05 2015

v1.0.2

1.0.2.0

Symfony validator wrapper of ronanguilloux/isocodes

  Sources   Download

MIT

The Requires

 

The Development Requires

validator symfony iso codes

30/04 2015

v1.0.1

1.0.1.0

Symfony validator wrapper of ronanguilloux/isocodes

  Sources   Download

MIT

The Requires

 

The Development Requires

validator symfony iso codes

23/04 2015

v1.0.0

1.0.0.0

Symfony validator wrapper of ronanguilloux/isocodes

  Sources   Download

MIT

The Requires

 

The Development Requires

validator symfony iso codes