10000 [Validator] Allow single constraint to be passed to the `constraints`… · symfony/symfony@af04cb5 · GitHub
[go: up one dir, main page]

Skip to content

Commit af04cb5

Browse files
[Validator] Allow single constraint to be passed to the constraints option of the When constraint
1 parent 877a8e8 commit af04cb5

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.0
5+
---
6+
7+
* Allow single constraint to be passed to the `constraints` option of the `When` constraint
8+
49
6.3
510
---
611

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

+7-1
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

+3
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

+27
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

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public function testConstraints AB14 AreExecuted()
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)
0