Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 460f660

Browse filesBrowse files
committed
add groups support to the Valid constraint
1 parent 61a67ec commit 460f660
Copy full SHA for 460f660

File tree

5 files changed

+99
-14
lines changed
Filter options

5 files changed

+99
-14
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Valid.php
+15-8Lines changed: 15 additions & 8 deletions
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
}
+37Lines changed: 37 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Mapping/GenericMetadata.php
+1-1Lines changed: 1 addition & 1 deletion
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php
+11-5Lines changed: 11 additions & 5 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Validator/AbstractTest.php
+35Lines changed: 35 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.