never directly validate Existence (Required/Optional) constraints

This commit is contained in:
Christian Flothmann 2020-05-21 21:40:39 +02:00
parent dd902d939f
commit d333aae187
2 changed files with 21 additions and 0 deletions

View File

@ -19,6 +19,8 @@ use Symfony\Component\Validator\Constraints\IsTrue;
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;
@ -157,4 +159,18 @@ class RecursiveValidatorTest extends AbstractTest
$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);
}
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Validator;
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;
@ -790,6 +791,10 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
$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) {