Skip to content

[Validator] Support enums in Choice constraints #43047

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

Closed
wants to merge 3 commits into from
Closed
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 @@ -5,6 +5,7 @@ CHANGELOG
---

* Add support for `ConstraintViolationList::createFromMessage()`
* Add enum support (>= PHP 8.1) for the `Choice` constraint

5.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, Choice::class);
}

if (!\is_array($constraint->choices) && !$constraint->callback) {
$valid = \is_array($constraint->choices)
|| (\is_string($constraint->choices) && is_a($constraint->choices, \BackedEnum::class, true))
|| isset($constraint->callback);

if (!$valid) {
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice.');
}

Expand All @@ -55,6 +59,8 @@ public function validate($value, Constraint $constraint)
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback.');
}
$choices = $choices();
} elseif (is_a($constraint->choices, \BackedEnum::class, true)) {
$choices = \array_column($constraint->choices::cases(), 'value');
} else {
$choices = $constraint->choices;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\SampleBackedEnum;

function choice_callback()
{
Expand Down Expand Up @@ -93,6 +94,10 @@ public function provideConstraintsWithChoicesArray(): iterable
if (\PHP_VERSION_ID >= 80000) {
yield 'named arguments' => [eval('return new \Symfony\Component\Validator\Constraints\Choice(choices: ["foo", "bar"]);')];
}

if (\PHP_VERSION_ID >= 80100) {
yield 'enums' => [new Choice(SampleBackedEnum::class)];
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Validator/Tests/SampleBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests;

enum SampleBackedEnum: string
{
case FOO = 'foo';
case BAR = 'bar';
}