2017 © Pedro Peláez
 

library symfony-validator-conditional

Symfony validator for conditional validations based on object properties

image

bcastellano/symfony-validator-conditional

Symfony validator for conditional validations based on object properties

  • Saturday, November 5, 2016
  • by bcastellano
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3,921 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 22 % Grown

The README.md

Build Status Coverage Status SensioLabsInsight License, (*1)

Symfony Conditional validator

Symfony validator for conditional validations based on object properties, (*2)

Install

The recommended way to install is through composer:, (*3)

$ composer require bcastellano/symfony-validator-conditional

or adding to require section in composer.json, (*4)

Usage

You can use PHP, Annotations, YAML or XML., (*5)

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)
    {
        // ...
    }
}

The Versions

05/11 2016

dev-master

9999999-dev

Symfony validator for conditional validations based on object properties

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

php validator symfony2 symfony3 symfony condition

05/11 2016

v1.0.1

1.0.1.0

Symfony validator for conditional validations based on object properties

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

php validator symfony2 symfony3 symfony condition

16/05 2016

1.0.0

1.0.0.0

Symfony validator for conditional validations based on object properties

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

php validator symfony2 symfony3 symfony condition