ibekiaris/zf2-annotation-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ibekiaris/zf2-annotation-validator

Composer 安装命令:

composer require ibekiaris/zf2-annotation-validator

包简介

Add validation annotations to doctrine entity

README 文档

README

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

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.

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

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

Installation

  1. With composer:
composer require ibekiaris/zf2-annotation-validator
  1. Directly in you ZF2 project:

Just copy module files into "module" directory

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

'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.

Documentation

Use AnnotationValidator\DoctrineValidatorTrait into you entities.

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.

e.g module.config.php

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:

e.g

/**
 * @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.

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 4
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD
  • 更新时间: 2015-10-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固