Skip to content

[Validator] add groups support to the Valid constraint #21111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
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 constant values and will throw an exception in Symfony 4.0
Expand Down
23 changes: 15 additions & 8 deletions src/Symfony/Component/Validator/Constraints/Valid.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not completely sure if handling the traversal really should be done here or if we should better add some special handling for the Valid constraint to the RecursiveContextualValidator instead. Any opinions are warmly welcome.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks clean to me like this.


foreach ($violations as $violation) {
$this->context->buildViolation($violation->getMessage(), $violation->getParameters())
->atPath($violation->getPropertyPath())
->addViolation();
}
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}