sllh / iso-codes-validator
Symfony validator wrapper of ronanguilloux/isocodes
Requires
- php: ^7.4 || ^8.0
- ronanguilloux/isocodes: ^2.3.4
- symfony/translation: ^4.4 || ^5.0
- symfony/validator: ^4.4 || ^5.0
Requires (Dev)
- cweagans/composer-patches: ^1.7
- matthiasnoback/symfony-dependency-injection-test: ^4.2
- sebastian/exporter: ^1.2.0
- symfony/dependency-injection: ^4.4 || ^5.0
- symfony/finder: ^4.4 || ^5.0
- symfony/http-kernel: ^4.4 || ^5.0
- symfony/phpunit-bridge: ^4.4 || ^5.0
Suggests
- symfony/dependency-injection: For Symfony integration as a bundle
- symfony/finder: For Symfony integration as a bundle
- symfony/http-kernel: For Symfony integration as a bundle
Conflicts
- symfony/dependency-injection: <2.7
- symfony/finder: <2.7
- symfony/http-kernel: <2.7
README
Symfony validator wrapper of IsoCodes project.
Installation
First of all, you need to require this library through composer:
$ composer require sllh/iso-codes-validator
After this, you can use it as is.
If you are using it on a Symfony project, you should read the following instructions for a better integration.
As a Symfony bundle
If your project is not using Symfony Full Stack, you must add the following dependencies:
$ 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.
# app/config/config.yml framework: translator: ~
Enable the bundle
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new SLLH\IsoCodesValidator\Bridge\Symfony\Bundle\SLLHIsoCodesValidatorBundle(), ); }
Usage
IsoCodesValidator is based on Symfony Validator library.
Manual usage
Create and use IsoCodes constraints by using the Symfony Validation class:
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.
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);
Constraints reference
Constraints classes can be found on src/Constraints.
All works "as is" without any options unless message
.
Only ZipCode constraint
accept a country
option to limit validation ('all' by default).
Please note that some IsoCodes classes are already implemented on Symfony Validator component. It's up to you to decide which one to use.
If you think an IsoCodes class is missing, feel free to open an issue or make a PR.
License
This bundle is under the MIT license. See the complete license on the LICENSE file.
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