From c59a6296e4ff27a9f7516f82c821872a05644a6b Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Thu, 25 May 2023 11:11:16 +0200 Subject: [PATCH] [Validator] Allow single constraint to be passed to the `constraints` option of the `When` constraint --- src/Symfony/Component/Validator/CHANGELOG.md | 1 + .../Component/Validator/Constraints/When.php | 8 +++++- .../Fixtures/WhenTestWithAttributes.php | 3 +++ .../Validator/Tests/Constraints/WhenTest.php | 27 +++++++++++++++++++ .../Tests/Constraints/WhenValidatorTest.php | 11 ++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 22e31fff2bb26..b971464397dc6 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Allow single integer for the `versions` option of the `Uuid` constraint + * Allow single constraint to be passed to the `constraints` option of the `When` constraint 6.3 --- diff --git a/src/Symfony/Component/Validator/Constraints/When.php b/src/Symfony/Component/Validator/Constraints/When.php index 12434319c66af..bf10049e47585 100644 --- a/src/Symfony/Component/Validator/Constraints/When.php +++ b/src/Symfony/Component/Validator/Constraints/When.php @@ -13,10 +13,12 @@ use Symfony\Component\ExpressionLanguage\Expression; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; +use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\LogicException; /** * @Annotation + * * @Target({"CLASS", "PROPERTY", "METHOD", "ANNOTATION"}) */ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] @@ -26,7 +28,7 @@ class When extends Composite public $constraints = []; public $values = []; - public function __construct(string|Expression|array $expression, array $constraints = null, array $values = null, array $groups = null, $payload = null, array $options = []) + public function __construct(string|Expression|array $expression, array|Constraint $constraints = null, array $values = null, array $groups = null, $payload = null, array $options = []) { if (!class_exists(ExpressionLanguage::class)) { throw new LogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" constraint. Try running "composer require symfony/expression-language".', __CLASS__)); @@ -39,6 +41,10 @@ public function __construct(string|Expression|array $expression, array $constrai $options['constraints'] = $constraints; } + if (isset($options['constraints']) && !\is_array($options['constraints'])) { + $options['constraints'] = [$options['constraints']]; + } + if (null !== $groups) { $options['groups'] = $groups; } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/WhenTestWithAttributes.php b/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/WhenTestWithAttributes.php index 3211f6c26a318..a683eb3c67940 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/WhenTestWithAttributes.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/WhenTestWithAttributes.php @@ -33,6 +33,9 @@ class WhenTestWithAttributes ], groups: ['foo'])] private $bar; + #[When(expression: 'true', constraints: new NotNull(), groups: ['foo'])] + private $qux; + #[When(expression: 'true', constraints: [ new NotNull(), new NotBlank(), diff --git a/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php b/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php index 91938140d8603..e057d6b0c43bc 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php @@ -88,6 +88,17 @@ public function testAnnotations() ], $barConstraint->constraints); self::assertSame(['foo'], $barConstraint->groups); + [$quxConstraint] = $metadata->properties['qux']->getConstraints(); + + self::assertInstanceOf(When::class, $quxConstraint); + self::assertSame('true', $quxConstraint->expression); + self::assertEquals([ + new NotNull([ + 'groups' => ['foo'], + ]), + ], $quxConstraint->constraints); + self::assertSame(['foo'], $quxConstraint->groups); + [$bazConstraint] = $metadata->getters['baz']->getConstraints(); self::assertInstanceOf(When::class, $bazConstraint); @@ -152,6 +163,17 @@ public function testAttributes() ], $barConstraint->constraints); self::assertSame(['foo'], $barConstraint->groups); + [$quxConstraint] = $metadata->properties['qux']->getConstraints(); + + self::assertInstanceOf(When::class, $quxConstraint); + self::assertSame('true', $quxConstraint->expression); + self::assertEquals([ + new NotNull([ + 'groups' => ['foo'], + ]), + ], $quxConstraint->constraints); + self::assertSame(['foo'], $quxConstraint->groups); + [$bazConstraint] = $metadata->getters['baz']->getConstraints(); self::assertInstanceOf(When::class, $bazConstraint); @@ -183,6 +205,11 @@ class WhenTestWithAnnotations */ private $bar; + /** + * @When(expression="true", constraints=@NotNull, groups={"foo"}) + */ + private $qux; + /** * @When(expression="true", constraints={@NotNull, @NotBlank}) */ diff --git a/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php index 5ced3de36ac68..cba81b25af34a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php @@ -39,6 +39,17 @@ public function testConstraintsAreExecuted() ])); } + public function testConstraintIsExecuted() + { + $constraint = new NotNull(); + $this->expectValidateValue(0, 'Foo', [$constraint]); + + $this->validator->validate('Foo', new When([ + 'expression' => 'true', + 'constraints' => $constraint, + ])); + } + public function testConstraintsAreExecutedWithNull() { $constraints = [ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy