From 54cb6e458e7b12492ecf443fa83e1e98f1680aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=B6rl?= Date: Sat, 14 Jan 2012 02:06:07 +0100 Subject: [PATCH] [Validator] Added dynamic constraints --- .../Validator/ConstraintProviderInterface.php | 27 ++++++++++++++++++ src/Symfony/Component/Validator/Validator.php | 4 +++ .../Fixtures/DynamicConstraintsEntity.php | 21 ++++++++++++++ .../Component/Validator/ValidatorTest.php | 28 +++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 src/Symfony/Component/Validator/ConstraintProviderInterface.php create mode 100644 tests/Symfony/Tests/Component/Validator/Fixtures/DynamicConstraintsEntity.php diff --git a/src/Symfony/Component/Validator/ConstraintProviderInterface.php b/src/Symfony/Component/Validator/ConstraintProviderInterface.php new file mode 100644 index 0000000000..4541b622e4 --- /dev/null +++ b/src/Symfony/Component/Validator/ConstraintProviderInterface.php @@ -0,0 +1,27 @@ + + * + * 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); +} diff --git a/src/Symfony/Component/Validator/Validator.php b/src/Symfony/Component/Validator/Validator.php index d20a623256..a8ec922b0f 100644 --- a/src/Symfony/Component/Validator/Validator.php +++ b/src/Symfony/Component/Validator/Validator.php @@ -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, ''); diff --git a/tests/Symfony/Tests/Component/Validator/Fixtures/DynamicConstraintsEntity.php b/tests/Symfony/Tests/Component/Validator/Fixtures/DynamicConstraintsEntity.php new file mode 100644 index 0000000000..ae30742616 --- /dev/null +++ b/tests/Symfony/Tests/Component/Validator/Fixtures/DynamicConstraintsEntity.php @@ -0,0 +1,21 @@ +addPropertyConstraint('firstValue', new FailingConstraint()); + $metadata->addGetterConstraint('secondValue', new FailingConstraint()); + } +} diff --git a/tests/Symfony/Tests/Component/Validator/ValidatorTest.php b/tests/Symfony/Tests/Component/Validator/ValidatorTest.php index 78b0386bfa..05ce9951c1 100644 --- a/tests/Symfony/Tests/Component/Validator/ValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/ValidatorTest.php @@ -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() {