Skip to content

Commit 54cb6e4

Browse files
committed
[Validator] Added dynamic constraints
1 parent 2a74ac3 commit 54cb6e4

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator;
13+
14+
use Symfony\Component\Validator\Mapping\ClassMetadata;
15+
16+
/**
17+
* Makes it possible to define dynamic constraints for an object.
18+
*/
19+
interface ConstraintProviderInterface
20+
{
21+
/**
22+
* Lets the user create dynamic constraints
23+
*
24+
* @param Mapping\ClassMetadata
25+
*/
26+
public function getConstraints(ClassMetadata $metadata);
27+
}

src/Symfony/Component/Validator/Validator.php

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function getMetadataFactory()
5757
public function validate($object, $groups = null)
5858
{
5959
$metadata = $this->metadataFactory->getClassMetadata(get_class($object));
60+
61+
if ($object instanceof ConstraintProviderInterface) {
62+
$object->getConstraints($metadata);
63+
}
6064

6165
$walk = function(GraphWalker $walker, $group) use ($metadata, $object) {
6266
return $walker->walkObject($metadata, $object, $group, '');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Tests\Component\Validator\Fixtures;
4+
5+
use Symfony\Component\Validator\ConstraintProviderInterface;
6+
use Symfony\Component\Validator\Mapping\ClassMetadata;
7+
8+
class DynamicConstraintsEntity implements ConstraintProviderInterface
9+
{
10+
protected $firstValue;
11+
12+
public function getSecondValue() {
13+
return null;
14+
}
15+
16+
public function getConstraints(ClassMetadata $metadata)
17+
{
18+
$metadata->addPropertyConstraint('firstValue', new FailingConstraint());
19+
$metadata->addGetterConstraint('secondValue', new FailingConstraint());
20+
}
21+
}

tests/Symfony/Tests/Component/Validator/ValidatorTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
require_once __DIR__.'/Fixtures/FailingConstraint.php';
1616
require_once __DIR__.'/Fixtures/FailingConstraintValidator.php';
1717
require_once __DIR__.'/Fixtures/FakeClassMetadataFactory.php';
18+
require_once __DIR__.'/Fixtures/DynamicConstraintsEntity.php';
1819

1920
use Symfony\Tests\Component\Validator\Fixtures\Entity;
2021
use Symfony\Tests\Component\Validator\Fixtures\FakeClassMetadataFactory;
2122
use Symfony\Tests\Component\Validator\Fixtures\FailingConstraint;
23+
use Symfony\Tests\Component\Validator\Fixtures\DynamicConstraintsEntity;
2224
use Symfony\Component\Validator\Validator;
2325
use Symfony\Component\Validator\ConstraintViolation;
2426
use Symfony\Component\Validator\ConstraintViolationList;
@@ -121,6 +123,32 @@ public function testValidate_multipleGroups()
121123

122124
$this->assertEquals($violations, $result);
123125
}
126+
127+
public function testValidate_constraintProvider()
128+
{
129+
$entity = new DynamicConstraintsEntity();
130+
$metadata = new ClassMetadata(get_class($entity));
131+
$this->factory->addClassMetadata($metadata);
132+
133+
// Only the constraint of group "Default" failed
134+
$violations = new ConstraintViolationList();
135+
$violations->add(new ConstraintViolation(
136+
'',
137+
array(),
138+
$entity,
139+
'firstValue',
140+
''
141+
));
142+
$violations->add(new ConstraintViolation(
143+
'',
144+
array(),
145+
$entity,
146+
'secondValue',
147+
''
148+
));
149+
150+
$this->assertEquals($violations, $this->validator->validate($entity));
151+
}
124152

125153
public function testValidateProperty()
126154
{

0 commit comments

Comments
 (0)