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 f78f106

Browse filesBrowse files
committed
add groups support to the Valid constraint
1 parent 7b59412 commit f78f106
Copy full SHA for f78f106

File tree

6 files changed

+104
-14
lines changed
Filter options

6 files changed

+104
-14
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

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

‎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
@@ -19,11 +19,17 @@
1919
*/
2020
class ValidTest extends TestCase
2121
{
22-
/**
23-
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
24-
*/
25-
public function testRejectGroupsOption()
22+
public function testGroupsCanBeSet()
2623
{
27-
new Valid(array('groups' => 'foo'));
24+
$constraint = new Valid(array('groups' => 'foo'));
25+
26+
$this->assertSame(array('foo'), $constraint->groups);
27+
}
28+
29+
public function testGroupsAreNullByDefault()
30+
{
31+
$constraint = new Valid();
32+
33+
$this->assertNull($constraint->groups);
2834
}
2935
}

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