use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Collection;
+use Symfony\Component\Validator\Constraints\Expression;
use Symfony\Component\Validator\Constraints\GroupSequence;
+use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Traverse;
use Symfony\Component\Validator\Constraints\Valid;
protected $validator;
/**
- * @param MetadataFactoryInterface $metadataFactory
- * @param array $objectInitializers
- *
* @return ValidatorInterface
*/
abstract protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = array());
$called = false;
$entity = new Entity();
$entity->firstName = 'Bernhard';
+ $entity->data = array('firstName' => 'Bernhard');
$callback = function ($value, ExecutionContextInterface $context) use ($entity, &$called) {
$called = true;
$this->metadata->addConstraint(new Callback($callback));
$this->metadata->addPropertyConstraint('firstName', new Callback($callback));
+ $this->metadata->addPropertyConstraint('data', new Collection(array('firstName' => new Expression('value == this.firstName'))));
$this->validator->validate($entity);
$this->assertCount(1, $violations);
$this->assertSame($constraint, $violations[0]->getConstraint());
}
+
+ public function testNestedObjectIsNotValidatedIfGroupInValidConstraintIsNotValidated()
+ {
+ $entity = new Entity();
+ $entity->firstName = '';
+ $reference = new Reference();
+ $reference->value = '';
+ $entity->childA = $reference;
+
+ $this->metadata->addPropertyConstraint('firstName', new NotBlank(array('groups' => 'group1')));
+ $this->metadata->addPropertyConstraint('childA', new Valid(array('groups' => 'group1')));
+ $this->referenceMetadata->addPropertyConstraint('value', new NotBlank());
+
+ $violations = $this->validator->validate($entity, null, array());
+
+ $this->assertCount(0, $violations);
+ }
+
+ public function testNestedObjectIsValidatedIfGroupInValidConstraintIsValidated()
+ {
+ $entity = new Entity();
+ $entity->firstName = '';
+ $reference = new Reference();
+ $reference->value = '';
+ $entity->childA = $reference;
+
+ $this->metadata->addPropertyConstraint('firstName', new NotBlank(array('groups' => 'group1')));
+ $this->metadata->addPropertyConstraint('childA', new Valid(array('groups' => 'group1')));
+ $this->referenceMetadata->addPropertyConstraint('value', new NotBlank(array('groups' => 'group1')));
+
+ $violations = $this->validator->validate($entity, null, array('Default', 'group1'));
+
+ $this->assertCount(2, $violations);
+ }
}