Skip to content

Commit

Permalink
feature #21111 [Validator] add groups support to the Valid constraint…
Browse files Browse the repository at this point in the history
… (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] add groups support to the Valid constraint

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #3622, #17622
| License       | MIT
| Doc PR        | TODO

Commits
-------

0ca27cc add groups support to the Valid constraint
  • Loading branch information
fabpot committed Aug 5, 2017
2 parents 73d3d5a + 0ca27cc commit a12ebf7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
3.4.0
-----

* added support for validation groups to the `Valid` constraint
* not setting the `strict` option of the `Choice` constraint to `true` is
deprecated and will throw an exception in Symfony 4.0
* setting the `checkDNS` option of the `Url` constraint to `true` is deprecated in favor of
Expand Down
23 changes: 15 additions & 8 deletions src/Symfony/Component/Validator/Constraints/Valid.php
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
* @Annotation
Expand All @@ -24,15 +23,23 @@ class Valid extends Constraint
{
public $traverse = true;

public function __construct($options = null)
public function __get($option)
{
if (is_array($options) && array_key_exists('groups', $options)) {
throw new ConstraintDefinitionException(sprintf(
'The option "groups" is not supported by the constraint %s',
__CLASS__
));
if ('groups' === $option) {
// when this is reached, no groups have been configured
return null;
}

parent::__construct($options);
return parent::__get($option);
}

/**
* {@inheritdoc}
*/
public function addImplicitGroupName($group)
{
if (null !== $this->groups) {
parent::addImplicitGroupName($group);
}
}
}
37 changes: 37 additions & 0 deletions src/Symfony/Component/Validator/Constraints/ValidValidator.php
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
*/
class ValidValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Valid) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
}

$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));

foreach ($violations as $violation) {
$this->context->buildViolation($violation->getMessage(), $violation->getParameters())
->atPath($violation->getPropertyPath())
->addViolation();
}
}
}
Expand Up @@ -131,7 +131,7 @@ public function addConstraint(Constraint $constraint)
));
}

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

if ($constraint->traverse) {
Expand Down
16 changes: 11 additions & 5 deletions src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php
Expand Up @@ -19,11 +19,17 @@
*/
class ValidTest extends TestCase
{
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testRejectGroupsOption()
public function testGroupsCanBeSet()
{
new Valid(array('groups' => 'foo'));
$constraint = new Valid(array('groups' => 'foo'));

$this->assertSame(array('foo'), $constraint->groups);
}

public function testGroupsAreNullByDefault()
{
$constraint = new Valid();

$this->assertNull($constraint->groups);
}
}
35 changes: 35 additions & 0 deletions src/Symfony/Component/Validator/Tests/Validator/AbstractTest.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Collection;
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;
Expand Down Expand Up @@ -670,4 +671,38 @@ public function testCollectionConstraitViolationHasCorrectContext()
$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);
}
}

0 comments on commit a12ebf7

Please sign in to comment.