Skip to content

Commit

Permalink
bug #36894 [Validator] never directly validate Existence (Required/Op…
Browse files Browse the repository at this point in the history
…tional) constraints (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] never directly validate Existence (Required/Optional) constraints

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36637 #36723
| License       | MIT
| Doc PR        |

Using `Optional` or `Required` like "regular" constraints does not make any sense, but doing so didn't break before #36365. I suggest to ignore them for now and deprecate using them outside the `Collection` constraint in 5.2.

Commits
-------

d333aae never directly validate Existence (Required/Optional) constraints
  • Loading branch information
nicolas-grekas committed May 30, 2020
2 parents 8e40814 + d333aae commit 84081ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Expand Up @@ -19,6 +19,8 @@
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Mapping\ClassMetadata;
Expand Down Expand Up @@ -157,4 +159,18 @@ public function testAllConstraintValidateAllGroupsForNestedConstraints()
$this->assertInstanceOf(NotBlank::class, $violations->get(0)->getConstraint());
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
}

public function testRequiredConstraintIsIgnored()
{
$violations = $this->validator->validate([], new Required());

$this->assertCount(0, $violations);
}

public function testOptionalConstraintIsIgnored()
{
$violations = $this->validator->validate([], new Optional());

$this->assertCount(0, $violations);
}
}
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Composite;
use Symfony\Component\Validator\Constraints\Existence;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
Expand Down Expand Up @@ -790,6 +791,10 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
$context->setGroup($group);

foreach ($metadata->findConstraints($group) as $constraint) {
if ($constraint instanceof Existence) {
continue;
}

// Prevent duplicate validation of constraints, in the case
// that constraints belong to multiple validated groups
if (null !== $cacheKey) {
Expand Down

0 comments on commit 84081ad

Please sign in to comment.