定制 bcastellano/symfony-validator-conditional 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

bcastellano/symfony-validator-conditional

最新稳定版本:v1.0.1

Composer 安装命令:

composer require bcastellano/symfony-validator-conditional

包简介

Symfony validator for conditional validations based on object properties

README 文档

README

Build Status Coverage Status SensioLabsInsight License

Symfony Conditional validator

Symfony validator for conditional validations based on object properties

Install

The recommended way to install is through composer:

$ composer require bcastellano/symfony-validator-conditional

or adding to require section in composer.json

Usage

You can use PHP, Annotations, YAML or XML.

Configuration example with PHP

// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;

use Bcastellano\Symfony\Validator\Constraints\Conditional;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class User
{
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        // property validator usage
        $metadata->addPropertyConstraint('name', new Conditional(array(
            'constraints' => array(
                new Assert\NotBlank(),
            ),
            'condition' => function($value){
                
                // add login here... $value is object of this property and can be use to check context 
                
                return $boolean; 
            }
        )));
        
        // class validator usage
        $metadata->addConstraint(new Conditional(array(
            'constraints' => array(
                new Assert\Callback('validate'),
            ),
            'condition' => function($value){
                             
                // add login here... $value is object validating and can be use to check context
             
                return $boolean; 
            }
        )));
    }
}

Configuration example with annotations

// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;

use Bcasellano\Symfony\Validator\Constraints\Conditional;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
 * Class validator
 *
 * @Conditional(
 *     constraints = {
 *         @Assert\Callback({"AppBundle\Entity\User","validate"})
 *     },
 *     condition = "AppBundle\Entity\User::shouldValidateName"
 * )
 */
class User
{
    /**
     * Property validator
     *
     * @Conditional(
     *     constraints = {
     *         @Assert\NotBlank()
     *     },
     *     condition = "AppBundle\Entity\User::shouldValidateName"
     * )
     */
    protected $name;

    public static function shouldValidateName($object)
    {
        // add login here... $value is object validating and can be use to check context
                 
        return $boolean;
    }
    
    
    public static function validate($object, ExecutionContextInterface $context, $payload)
    {
        // ...
    }
}

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-15