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 8f4e98c

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

File tree

5 files changed

+100
-13
lines changed
Filter options

5 files changed

+100
-13
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Valid.php
+15-7Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,23 @@ class Valid extends Constraint
2424
{
2525
public $traverse = true;
2626

27-
public function __construct($options = null)
27+
public function __get($option)
2828
{
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-
));
29+
if ('groups' === $option) {
30+
// when this is reached, no groups have been configured
31+
return null;
3432
}
3533

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

‎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.