Skip to content

Commit

Permalink
never directly validate Existence (Required/Optional) constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed May 22, 2020
1 parent dd902d9 commit d333aae
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 d333aae

Please sign in to comment.