Skip to content

Commit 5fb8812

Browse files
bug #36894 [Validator] never directly validate Existence (Required/Optional) 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 ------- d333aae187 never directly validate Existence (Required/Optional) constraints
2 parents 3c817de + e625da8 commit 5fb8812

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Tests/Validator/RecursiveValidatorTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\Validator\Constraints\Length;
2020
use Symfony\Component\Validator\Constraints\NotBlank;
2121
use Symfony\Component\Validator\Constraints\NotNull;
22+
use Symfony\Component\Validator\Constraints\Optional;
23+
use Symfony\Component\Validator\Constraints\Required;
2224
use Symfony\Component\Validator\ConstraintValidatorFactory;
2325
use Symfony\Component\Validator\Context\ExecutionContextFactory;
2426
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -157,4 +159,18 @@ public function testAllConstraintValidateAllGroupsForNestedConstraints()
157159
$this->assertInstanceOf(NotBlank::class, $violations->get(0)->getConstraint());
158160
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
159161
}
162+
163+
public function testRequiredConstraintIsIgnored()
164+
{
165+
$violations = $this->validator->validate([], new Required());
166+
167+
$this->assertCount(0, $violations);
168+
}
169+
170+
public function testOptionalConstraintIsIgnored()
171+
{
172+
$violations = $this->validator->validate([], new Optional());
173+
174+
$this->assertCount(0, $violations);
175+
}
160176
}

Validator/RecursiveContextualValidator.php

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\Constraints\Composite;
16+
use Symfony\Component\Validator\Constraints\Existence;
1617
use Symfony\Component\Validator\Constraints\GroupSequence;
1718
use Symfony\Component\Validator\Constraints\Valid;
1819
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
@@ -790,6 +791,10 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
790791
$context->setGroup($group);
791792

792793
foreach ($metadata->findConstraints($group) as $constraint) {
794+
if ($constraint instanceof Existence) {
795+
continue;
796+
}
797+
793798
// Prevent duplicate validation of constraints, in the case
794799
// that constraints belong to multiple validated groups
795800
if (null !== $cacheKey) {

0 commit comments

Comments
 (0)