Skip to content

Commit c59a629

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[Validator] Allow single constraint to be passed to the constraints option of the When constraint
1 parent 73862da commit c59a629

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Allow single integer for the `versions` option of the `Uuid` constraint
8+
* Allow single constraint to be passed to the `constraints` option of the `When` constraint
89

910
6.3
1011
---

src/Symfony/Component/Validator/Constraints/When.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use Symfony\Component\ExpressionLanguage\Expression;
1515
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16+
use Symfony\Component\Validator\Constraint;
1617
use Symfony\Component\Validator\Exception\LogicException;
1718

1819
/**
1920
* @Annotation
21+
*
2022
* @Target({"CLASS", "PROPERTY", "METHOD", "ANNOTATION"})
2123
*/
2224
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
@@ -26,7 +28,7 @@ class When extends Composite
2628
public $constraints = [];
2729
public $values = [];
2830

29-
public function __construct(string|Expression|array $expression, array $constraints = null, array $values = null, array $groups = null, $payload = null, array $options = [])
31+
public function __construct(string|Expression|array $expression, array|Constraint $constraints = null, array $values = null, array $groups = null, $payload = null, array $options = [])
3032
{
3133
if (!class_exists(ExpressionLanguage::class)) {
3234
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
3941
$options['constraints'] = $constraints;
4042
}
4143

44+
if (isset($options['constraints']) && !\is_array($options['constraints'])) {
45+
$options['constraints'] = [$options['constraints']];
46+
}
47+
4248
if (null !== $groups) {
4349
$options['groups'] = $groups;
4450
}

src/Symfony/Component/Validator/Tests/Constraints/Fixtures/WhenTestWithAttributes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class WhenTestWithAttributes
3333
], groups: ['foo'])]
3434
private $bar;
3535

36+
#[When(expression: 'true', constraints: new NotNull(), groups: ['foo'])]
37+
private $qux;
38+
3639
#[When(expression: 'true', constraints: [
3740
new NotNull(),
3841
new NotBlank(),

src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ public function testAnnotations()
8888
], $barConstraint->constraints);
8989
self::assertSame(['foo'], $barConstraint->groups);
9090

91+
[$quxConstraint] = $metadata->properties['qux']->getConstraints();
92+
93+
self::assertInstanceOf(When::class, $quxConstraint);
94+
self::assertSame('true', $quxConstraint->expression);
95+
self::assertEquals([
96+
new NotNull([
97+
'groups' => ['foo'],
98+
]),
99+
], $quxConstraint->constraints);
100+
self::assertSame(['foo'], $quxConstraint->groups);
101+
91102
[$bazConstraint] = $metadata->getters['baz']->getConstraints();
92103

93104
self::assertInstanceOf(When::class, $bazConstraint);
@@ -152,6 +163,17 @@ public function testAttributes()
152163
], $barConstraint->constraints);
153164
self::assertSame(['foo'], $barConstraint->groups);
154165

166+
[$quxConstraint] = $metadata->properties['qux']->getConstraints();
167+
168+
self::assertInstanceOf(When::class, $quxConstraint);
169+
self::assertSame('true', $quxConstraint->expression);
170+
self::assertEquals([
171+
new NotNull([
172+
'groups' => ['foo'],
173+
]),
174+
], $quxConstraint->constraints);
175+
self::assertSame(['foo'], $quxConstraint->groups);
176+
155177
[$bazConstraint] = $metadata->getters['baz']->getConstraints();
156178

157179
self::assertInstanceOf(When::class, $bazConstraint);
@@ -183,6 +205,11 @@ class WhenTestWithAnnotations
183205
*/
184206
private $bar;
185207

208+
/**
209+
* @When(expression="true", constraints=@NotNull, groups={"foo"})
210+
*/
211+
private $qux;
212+
186213
/**
187214
* @When(expression="true", constraints={@NotNull, @NotBlank})
188215
*/

src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public function testConstraintsAreExecuted()
3939
]));
4040
}
4141

42+
public function testConstraintIsExecuted()
43+
{
44+
$constraint = new NotNull();
45+
$this->expectValidateValue(0, 'Foo', [$constraint]);
46+
47+
$this->validator->validate('Foo', new When([
48+
'expression' => 'true',
49+
'constraints' => $constraint,
50+
]));
51+
}
52+
4253
public function testConstraintsAreExecutedWithNull()
4354
{
4455
$constraints = [

0 commit comments

Comments
 (0)
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