diff --git a/src/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php b/src/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php index 24dd50450f4..ff4b194aa0e 100644 --- a/src/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php +++ b/src/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php @@ -17,6 +17,7 @@ use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface as ValidatorMetadataFactoryInterface; +use Symfony\Component\Validator\Mapping\PropertyMetadataInterface as ValidatorPropertyMetadataInterface; /** * Decorates a metadata loader using the validator. @@ -39,24 +40,6 @@ public function __construct(ValidatorMetadataFactoryInterface $validatorMetadata $this->decorated = $decorated; } - /** - * Is this constraint making the related property required? - * - * @param Constraint $constraint - * - * @return bool - */ - private function isRequired(Constraint $constraint) : bool - { - foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) { - if ($constraint instanceof $requiredConstraint) { - return true; - } - } - - return false; - } - /** * {@inheritdoc} */ @@ -68,22 +51,9 @@ public function create(string $resourceClass, string $name, array $options = []) } $validatorClassMetadata = $this->validatorMetadataFactory->getMetadataFor($resourceClass); - foreach ($validatorClassMetadata->getPropertyMetadata($name) as $validatorPropertyMetadata) { if (isset($options['validation_groups'])) { - foreach ($options['validation_groups'] as $validationGroup) { - if (!is_string($validationGroup)) { - continue; - } - - foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $constraint) { - if ($this->isRequired($constraint)) { - return $propertyMetadata->withRequired(true); - } - } - } - - return $propertyMetadata->withRequired(false); + return $propertyMetadata->withRequired($this->isRequiredByGroups($validatorPropertyMetadata, $options)); } foreach ($validatorPropertyMetadata->findConstraints($validatorClassMetadata->getDefaultGroup()) as $constraint) { @@ -97,4 +67,47 @@ public function create(string $resourceClass, string $name, array $options = []) return $propertyMetadata->withRequired(false); } + + /** + * Tests if the property is required because of its validation groups. + * + * @param ValidatorPropertyMetadataInterface $validatorPropertyMetadata + * @param array $options + * + * @return bool|null + */ + private function isRequiredByGroups(ValidatorPropertyMetadataInterface $validatorPropertyMetadata, array $options) + { + foreach ($options['validation_groups'] as $validationGroup) { + if (!is_string($validationGroup)) { + continue; + } + + foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $constraint) { + if ($this->isRequired($constraint)) { + return true; + } + } + } + + return false; + } + + /** + * Is this constraint making the related property required? + * + * @param Constraint $constraint + * + * @return bool + */ + private function isRequired(Constraint $constraint) : bool + { + foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) { + if ($constraint instanceof $requiredConstraint) { + return true; + } + } + + return false; + } }