[Validator] Added dynamic constraints
This commit is contained in:
parent
2a74ac31d2
commit
54cb6e458e
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* Makes it possible to define dynamic constraints for an object.
|
||||
*/
|
||||
interface ConstraintProviderInterface
|
||||
{
|
||||
/**
|
||||
* Lets the user create dynamic constraints
|
||||
*
|
||||
* @param Mapping\ClassMetadata
|
||||
*/
|
||||
public function getConstraints(ClassMetadata $metadata);
|
||||
}
|
@ -57,6 +57,10 @@ class Validator implements ValidatorInterface
|
||||
public function validate($object, $groups = null)
|
||||
{
|
||||
$metadata = $this->metadataFactory->getClassMetadata(get_class($object));
|
||||
|
||||
if ($object instanceof ConstraintProviderInterface) {
|
||||
$object->getConstraints($metadata);
|
||||
}
|
||||
|
||||
$walk = function(GraphWalker $walker, $group) use ($metadata, $object) {
|
||||
return $walker->walkObject($metadata, $object, $group, '');
|
||||
|
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\Validator\Fixtures;
|
||||
|
||||
use Symfony\Component\Validator\ConstraintProviderInterface;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
|
||||
class DynamicConstraintsEntity implements ConstraintProviderInterface
|
||||
{
|
||||
protected $firstValue;
|
||||
|
||||
public function getSecondValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getConstraints(ClassMetadata $metadata)
|
||||
{
|
||||
$metadata->addPropertyConstraint('firstValue', new FailingConstraint());
|
||||
$metadata->addGetterConstraint('secondValue', new FailingConstraint());
|
||||
}
|
||||
}
|
@ -15,10 +15,12 @@ require_once __DIR__.'/Fixtures/Entity.php';
|
||||
require_once __DIR__.'/Fixtures/FailingConstraint.php';
|
||||
require_once __DIR__.'/Fixtures/FailingConstraintValidator.php';
|
||||
require_once __DIR__.'/Fixtures/FakeClassMetadataFactory.php';
|
||||
require_once __DIR__.'/Fixtures/DynamicConstraintsEntity.php';
|
||||
|
||||
use Symfony\Tests\Component\Validator\Fixtures\Entity;
|
||||
use Symfony\Tests\Component\Validator\Fixtures\FakeClassMetadataFactory;
|
||||
use Symfony\Tests\Component\Validator\Fixtures\FailingConstraint;
|
||||
use Symfony\Tests\Component\Validator\Fixtures\DynamicConstraintsEntity;
|
||||
use Symfony\Component\Validator\Validator;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
@ -121,6 +123,32 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals($violations, $result);
|
||||
}
|
||||
|
||||
public function testValidate_constraintProvider()
|
||||
{
|
||||
$entity = new DynamicConstraintsEntity();
|
||||
$metadata = new ClassMetadata(get_class($entity));
|
||||
$this->factory->addClassMetadata($metadata);
|
||||
|
||||
// Only the constraint of group "Default" failed
|
||||
$violations = new ConstraintViolationList();
|
||||
$violations->add(new ConstraintViolation(
|
||||
'',
|
||||
array(),
|
||||
$entity,
|
||||
'firstValue',
|
||||
''
|
||||
));
|
||||
$violations->add(new ConstraintViolation(
|
||||
'',
|
||||
array(),
|
||||
$entity,
|
||||
'secondValue',
|
||||
''
|
||||
));
|
||||
|
||||
$this->assertEquals($violations, $this->validator->validate($entity));
|
||||
}
|
||||
|
||||
public function testValidateProperty()
|
||||
{
|
||||
|
Reference in New Issue
Block a user