Skip to content

[Validator] Dynamic constraints #3114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Symfony/Component/Validator/ConstraintProviderInterface.php
Original file line number Diff line number Diff line change
@@ -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
*/
function loadDynamicValidatorMetadata(ClassMetadata $metadata);
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator;

use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* The default implementation of the ValidatorInterface.
Expand Down Expand Up @@ -57,6 +58,13 @@ public function getMetadataFactory()
public function validate($object, $groups = null)
{
$metadata = $this->metadataFactory->getClassMetadata(get_class($object));

if ($object instanceof ConstraintProviderInterface) {
$dynamicMetadata = new ClassMetadata(get_class($object));
$dynamicMetadata->mergeConstraints($metadata);
$object->loadDynamicValidatorMetadata($dynamicMetadata);
$metadata = $dynamicMetadata;
}

$walk = function(GraphWalker $walker, $group) use ($metadata, $object) {
return $walker->walkObject($metadata, $object, $group, '');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Symfony\Tests\Component\Validator\Fixtures;

use Symfony\Component\Validator\ConstraintProviderInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class DynamicConstraintsEntity implements ConstraintProviderInterface
{
protected $validationEnabled = false;
protected $firstValue;

public function setValidation($enabled)
{
$this->validationEnabled = $enabled;
}

public function getSecondValue()
{
return null;
}

public function loadDynamicValidatorMetadata(ClassMetadata $metadata)
{
if ($this->validationEnabled) {
$metadata->addPropertyConstraint('firstValue', new FailingConstraint());
$metadata->addGetterConstraint('secondValue', new FailingConstraint());
}
}
}
34 changes: 34 additions & 0 deletions tests/Symfony/Tests/Component/Validator/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
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;
Expand Down Expand Up @@ -121,6 +123,38 @@ public function testValidate_multipleGroups()

$this->assertEquals($violations, $result);
}

public function testValidate_constraintProvider()
{
$entity = new DynamicConstraintsEntity();
$metadata = new ClassMetadata(get_class($entity));
$this->factory->addClassMetadata($metadata);

$entity->setValidation(true);

$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
array(),
$entity,
'firstValue',
''
));
$violations->add(new ConstraintViolation(
'',
array(),
$entity,
'secondValue',
''
));

$this->assertEquals($violations, $this->validator->validate($entity));

$entity->setValidation(false);

$violations = new ConstraintViolationList();
$this->assertEquals($violations, $this->validator->validate($entity));
}

public function testValidateProperty()
{
Expand Down