2017 © Pedro Peláez
 

library zf2-annotation-validator

Add validation annotations to doctrine entity

image

ibekiaris/zf2-annotation-validator

Add validation annotations to doctrine entity

  • Wednesday, October 7, 2015
  • by ibekiaris
  • Repository
  • 4 Watchers
  • 4 Stars
  • 21 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Description

Annotation validator is a ZF2 module used to validate Doctrine Entities during their lifecycle call backs (events)., (*1)

By using Annotation Validator in your projects you can enhance productivity. At the same time, Junior developers who are involved in your project don't have to be concerned about validation since it takes place in entities.
You can also be sure that your persistence layer is more protected against code vulnerabilities., (*2)

At the moment data is validated just before flush, against the idea of using an ORM where entities must be valid after their "construction"., (*3)

However this module solved a lot of real life problems for me and I think that this is what matters when sharing code., (*4)

Installation

1) With composer:, (*5)

composer require ibekiaris/zf2-annotation-validator

2) Directly in you ZF2 project:, (*6)

Just copy module files into "module" directory, (*7)

In any case, after including code, add the following lines in application.config.php, (*8)

'modules' => array(
        'Application',
        'AnnotationValidator'
    ),

Finally add "module.config.sample.php" lines into your global or local project configuration file. You have to change of course 'validation_classes_aliases' according your needs., (*9)

Documentation

Use AnnotationValidator\DoctrineValidatorTrait into you entities., (*10)

use AnnotationValidator\DoctrineValidatorTrait

/**
 * This is a simple Entity Class
 *
 *
 * @ORM\Entity
 * @ORM\Table(name="clients")
 * @ORM\HasLifecycleCallbacks
 *
 * @author     Ioannis Bekiaris <bekiarisgiannis85@gmail.com>
 * @copyright  2015 - 2016 Ioannis Bekiaris
 */
class Client
{
    use DoctrineValidatorTrait;

    /**
     * @ORM\Column(name="title", type="string")
     *
     * @VLD {"type":"RequiredString"}
     * @var string
     */
    private $name;

}

Use @VLD annotation to validate $name property., (*11)

e.g module.config.php, (*12)

return [
    'validation_classes_aliases' => [
        'RequiredString' => [
            'validation_class' => '\Zend\Validator\Regex',
            'validation_options' => [
                'pattern' => '/^[\p{L}0-9\s\.\-]+$/u',
            ],
        ],
        'HostName' => [
            'validation_class' => '\Zend\Validator\Hostname',
            'validation_options' => [
                'allow' => \Zend\Validator\Hostname::ALLOW_ALL,
            ],
        ],
        'LonLat' => [
            'validation_class' => '\Zend\Validator\Regex',
            'validation_options' => [
                'pattern' => '/^[A-Z0-9\s\.]{0,10}$/',
             ],
        ],
    ],

NOTICES

Use "NotRequired" as suffix or prefix in validation types(aliases) to accept null as values:, (*13)

e.g, (*14)

/**
 * @ORM\Column(name="facebook_account_url", type="string", nullable=true)
 *
 * @VLD {"type":"UriNotRequired"}
 * @var string
 */
protected $facebookAccountUrl;

LICENSE

The files in this archive are released under license. You can find a copy of this license in LICENSE.txt., (*15)

The Versions

07/10 2015

dev-master

9999999-dev https://github.com/ibekiaris

Add validation annotations to doctrine entity

  Sources   Download

BSD 3-Clause

The Requires

 

by Ioannis Bekiaris

validation zf2 annotations doctrine