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

Skip to content

Commit 81a6786

Browse files
[Validator] Allow single constraint to be passed to the constraints option of the When constraint
1 parent a05197b commit 81a6786

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

UPGRADE-7.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ release process, both versions will have the same features, but Symfony 7.0 won'
66
To upgrade, make sure to resolve all deprecation notices.
77

88
This file will be updated on the branch 7.0 for each deprecated feature that is remov 10000 ed.
9+
10+
Validator
11+
---------
12+
13+
* Changed `$constraints` parameter type of the `When::__construct()` method from `array` to `array|Constraint`.

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
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

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);
@@ -149,6 +160,17 @@ public function testAttributes()
149160
], $barConstraint->constraints);
150161
self::assertSame(['foo'], $barConstraint->groups);
151162

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

154176
self::assertInstanceOf(When::class, $bazConstraint);
@@ -180,6 +202,11 @@ class WhenTestWithAnnotations
180202
*/
181203
private $bar;
182204

205+
/**
206+
* @When(expression="true", constraints=@NotNull, groups={"foo"})
207+
*/
208+
private $qux;
209+
183210
/**
184211
* @When(expression="true", constraints={@NotNull, @NotBlank})
185212
*/

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