8000 feature #45977 [Validator] Add the match option to the Choice constra… · symfony/symfony@303ea78 · GitHub
[go: up one dir, main page]

Skip to content

Commit 303ea78

Browse files
committed
feature #45977 [Validator] Add the match option to the Choice constraint (fancyweb)
This PR was merged into the 6.2 branch. Discussion ---------- [Validator] Add the match option to the Choice constraint | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #26074 | License | MIT | Doc PR | - I'd like to add a `match` option like the one in the `Regex` constraint to validate that a value is **not** in a given set of choices. Reusing the `Choice` constraint with a flag instead of creating the `NotChoice` constraint looks way better to me. Commits ------- 0f7f4aa [Validator] Add the match option to the Choice constraint
2 parents ab3d9fc + 0f7f4aa commit 303ea78

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
* Deprecate constraint `ExpressionLanguageSyntax`, use `ExpressionSyntax` instead
1515
* Add method `__toString()` to `ConstraintViolationInterface` & `ConstraintViolationListInterface`
1616
* Allow creating constraints with required arguments
17+
* Add the `match` option to the `Choice` constraint
1718

1819
6.0
1920
---

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Choice extends Constraint
4747
public $multipleMessage = 'One or more of the given values is invalid.';
4848
public $minMessage = 'You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.';
4949
public $maxMessage = 'You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.';
50+
public bool $match = true;
5051

5152
/**
5253
* {@inheritdoc}
@@ -69,7 +70,8 @@ public function __construct(
6970
string $minMessage = null,
7071
string $maxMessage = null,
7172
array $groups = null,
72-
mixed $payload = null
73+
mixed $payload = null,
74+
bool $match = null,
7375
) {
7476
if (\is_array($options) && $options && array_is_list($options)) {
7577
$choices ??= $options;
@@ -90,5 +92,6 @@ public function __construct(
9092
$this->multipleMessage = $multipleMessage ?? $this->multipleMessage;
9193
$this->minMessage = $minMessage ?? $this->minMessage;
9294
$this->maxMessage = $maxMessage ?? $this->maxMessage;
95+
$this->match = $match ?? $this->match;
9396
}
9497
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function validate(mixed $value, Constraint $constraint)
6565

6666
if ($constraint->multiple) {
6767
foreach ($value as $_value) {
68-
if (!\in_array($_value, $choices, true)) {
68+
if ($constraint->match xor \in_array($_value, $choices, true)) {
6969
$this->context->buildViolation($constraint->multipleMessage)
7070
->setParameter('{{ value }}', $this->formatValue($_value))
7171
->setParameter('{{ choices }}', $this->formatValues($choices))
@@ -98,7 +98,7 @@ public function validate(mixed $value, Constraint $constraint)
9898

9999
return;
100100
}
101-
} elseif (!\in_array($value, $choices, true)) {
101+
} elseif ($constraint->match xor \in_array($value, $choices, true)) {
102102
$this->context->buildViolation($constraint->message)
103103
->setParameter('{{ value }}', $this->formatValue($value))
104104
->setParameter('{{ choices }}', $this->formatValues($choices))

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,34 @@ public function testStrictWithMultipleChoices()
343343
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
344344
->assertRaised();
345345
}
346+
347+
public function testMatchFalse()
348+
{
349+
$this->validator->validate('foo', new Choice([
350+
'choices' => ['foo', 'bar'],
351+
'match' => false,
352+
]));
353+
354+
$this->buildViolation('The value you selected is not a valid choice.')
355+
->setParameter('{{ value }}', '"foo"')
356+
->setParameter('{{ choices }}', '"foo", "bar"')
357+
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
358+
->assertRaised();
359+
}
360+
361+
public function testMatchFalseWithMultiple()
362+
{
363+
$this->validator->validate(['ccc', 'bar', 'zzz'], new Choice([
364+
'choices' => ['foo', 'bar'],
365+
'multiple' => true,
366+
'match' => false,
367+
]));
368+
369+
$this->buildViolation('One or more of the given values is invalid.')
370+
->setParameter('{{ value }}', '"bar"')
371+
->setParameter('{{ choices }}', '"foo", "bar"')
372+
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
373+
->setInvalidValue('bar')
374+
->assertRaised();
375+
}
346376
}

0 commit comments

Comments
 (0)
0