Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Add the match option to the Choice constraint #45977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Deprecate constraint `ExpressionLanguageSyntax`, use `ExpressionSyntax` instead
* Add method `__toString()` to `ConstraintViolationInterface` & `ConstraintViolationListInterface`
* Allow creating constraints with required arguments
* Add the `match` option to the `Choice` constraint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to read the description of the PR to get what this was for.
Maybe it's a naming issue? Would negate be better instead? I'm not sure, just trying :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another possible naming is shouldMatch, as it's more clear to understand that a boolean is expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've spent some time thinking about this. I didn't find any reasonable alternative. The whole concept seems like an edge case anyway, so let's keep match (at least, this is the same name as for Regexp). Doc should be well written though :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And of course, if anyone has a better name before 6.2 final, we will be able to change it quickly.
cc @symfony/mergers


6.0
---
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Validator/Constraints/Choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Choice extends Constraint
public $multipleMessage = 'One or more of the given values is invalid.';
public $minMessage = 'You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.';
public $maxMessage = 'You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.';
public bool $match = true;

/**
* {@inheritdoc}
Expand All @@ -69,7 +70,8 @@ public function __construct(
string $minMessage = null,
string $maxMessage = null,
array $groups = null,
mixed $payload = null
mixed $payload = null,
bool $match = null,
) {
if (\is_array($options) && $options && array_is_list($options)) {
$choices ??= $options;
Expand All @@ -90,5 +92,6 @@ public function __construct(
$this->multipleMessage = $multipleMessage ?? $this->multipleMessage;
$this->minMessage = $minMessage ?? $this->minMessage;
$this->maxMessage = $maxMessage ?? $this->maxMessage;
$this->match = $match ?? $this->match;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function validate(mixed $value, Constraint $constraint)

if ($constraint->multiple) {
foreach ($value as $_value) {
if (!\in_array($_value, $choices, true)) {
if ($constraint->match xor \in_array($_value, $choices, true)) {
$this->context->buildViolation($constraint->multipleMessage)
->setParameter('{{ value }}', $this->formatValue($_value))
->setParameter('{{ choices }}', $this->formatValues($choices))
Expand Down Expand Up @@ -98,7 +98,7 @@ public function validate(mixed $value, Constraint $constraint)

return;
}
} elseif (!\in_array($value, $choices, true)) {
} elseif ($constraint->match xor \in_array($value, $choices, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ choices }}', $this->formatValues($choices))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,34 @@ public function testStrictWithMultipleChoices()
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->assertRaised();
}

public function testMatchFalse()
{
$this->validator->validate('foo', new Choice([
'choices' => ['foo', 'bar'],
'match' => false,
]));

$this->buildViolation('The value you selected is not a valid choice.')
->setParameter('{{ value }}', '"foo"')
->setParameter('{{ choices }}', '"foo", "bar"')
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->assertRaised();
}

public function testMatchFalseWithMultiple()
{
$this->validator->validate(['ccc', 'bar', 'zzz'], new Choice([
'choices' => ['foo', 'bar'],
'multiple' => true,
'match' => false,
]));

$this->buildViolation('One or more of the given values is invalid.')
->setParameter('{{ value }}', '"bar"')
->setParameter('{{ choices }}', '"foo", "bar"')
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->setInvalidValue('bar')
->assertRaised();
}
}