Skip to content

Commit cda1f2b

Browse files
committed
add groups support to the Valid constraint
1 parent 10c9d19 commit cda1f2b

File tree

6 files changed

+104
-14
lines changed

6 files changed

+104
-14
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* added support for validation groups to the `Valid` constraint
8+
49
3.2.0
510
-----
611

src/Symfony/Component/Validator/Constraints/Valid.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1615

1716
/**
1817
* @Annotation
@@ -24,15 +23,23 @@ class Valid extends Constraint
2423
{
2524
public $traverse = true;
2625

27-
public function __construct($options = null)
26+
public function __get($option)
2827
{
29-
if (is_array($options) && array_key_exists('groups', $options)) {
30-
throw new ConstraintDefinitionException(sprintf(
31-
'The option "groups" is not supported by the constraint %s',
32-
__CLASS__
33-
));
28+
if ('groups' === $option) {
29+
// when this is reached, no groups have been configured
30+
return null;
3431
}
3532

36-
parent::__construct($options);
33+
return parent::__get($option);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function addImplicitGroupName($group)
40+
{
41+
if (null !== $this->groups) {
42+
parent::addImplicitGroupName($group);
43+
}
3744
}
3845
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
20+
*/
21+
class ValidValidator extends ConstraintValidator
22+
{
23+
public function validate($value, Constraint $constraint)
24+
{
25+
if (!$constraint instanceof Valid) {
26+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
27+
}
28+
29+
$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));
30+
31+
foreach ($violations as $violation) {
32+
$this->context->buildViolation($violation->getMessage(), $violation->getParameters())
33+
->atPath($violation->getPropertyPath())
34+
->addViolation();
35+
}
36+
}
37+
}

src/Symfony/Component/Validator/Mapping/GenericMetadata.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function addConstraint(Constraint $constraint)
131131
));
132132
}
133133

134-
if ($constraint instanceof Valid) {
134+
if ($constraint instanceof Valid && null === $constraint->groups) {
135135
$this->cascadingStrategy = CascadingStrategy::CASCADE;
136136

137137
if ($constraint->traverse) {

src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
*/
1919
class ValidTest extends \PHPUnit_Framework_TestCase
2020
{
21-
/**
22-
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
23-
*/
24-
public function testRejectGroupsOption()
21+
public function testGroupsCanBeSet()
2522
{
26-
new Valid(array('groups' => 'foo'));
23+
$constraint = new Valid(array('groups' => 'foo'));
24+
25+
$this->assertSame(array('foo'), $constraint->groups);
26+
}
27+
28+
public function testGroupsAreNullByDefault()
29+
{
30+
$constraint = new Valid();
31+
32+
$this->assertNull($constraint->groups);
2733
}
2834
}

src/Symfony/Component/Validator/Tests/Validator/AbstractTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraints\Callback;
1515
use Symfony\Component\Validator\Constraints\Collection;
1616
use Symfony\Component\Validator\Constraints\GroupSequence;
17+
use Symfony\Component\Validator\Constraints\NotBlank;
1718
use Symfony\Component\Validator\Constraints\NotNull;
1819
use Symfony\Component\Validator\Constraints\Traverse;
1920
use Symfony\Component\Validator\Constraints\Valid;
@@ -670,4 +671,38 @@ public function testCollectionConstraitViolationHasCorrectContext()
670671
$this->assertCount(1, $violations);
671672
$this->assertSame($constraint, $violations[0]->getConstraint());
672673
}
674+
675+
public function testNestedObjectIsNotValidatedIfGroupInValidConstraintIsNotValidated()
676+
{
677+
$entity = new Entity();
678+
$entity->firstName = '';
679+
$reference = new Reference();
680+
$reference->value = '';
681+
$entity->childA = $reference;
682+
683+
$this->metadata->addPropertyConstraint('firstName', new NotBlank(array('groups' => 'group1')));
684+
$this->metadata->addPropertyConstraint('childA', new Valid(array('groups' => 'group1')));
685+
$this->referenceMetadata->addPropertyConstraint('value', new NotBlank());
686+
687+
$violations = $this->validator->validate($entity, null, array());
688+
689+
$this->assertCount(0, $violations);
690+
}
691+
692+
public function testNestedObjectIsValidatedIfGroupInValidConstraintIsValidated()
693+
{
694+
$entity = new Entity();
695+
$entity->firstName = '';
696+
$reference = new Reference();
697+
$reference->value = '';
698+
$entity->childA = $reference;
699+
700+
$this->metadata->addPropertyConstraint('firstName', new NotBlank(array('groups' => 'group1')));
701+
$this->metadata->addPropertyConstraint('childA', new Valid(array('groups' => 'group1')));
702+
$this->referenceMetadata->addPropertyConstraint('value', new NotBlank(array('groups' => 'group1')));
703+
704+
$violations = $this->validator->validate($entity, null, array('Default', 'group1'));
705+
706+
$this->assertCount(2, $violations);
707+
}
673708
}

0 commit comments

Comments
 (0)