diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index f73923bc1f1f4..4fe89e7c775f1 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Add support for `ConstraintViolationList::createFromMessage()` + * Add enum support (>= PHP 8.1) for the `Choice` constraint 5.3 --- diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index a1c47a6bb22ed..169eec74aee0c 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -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.'); } @@ -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; } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 262e8654f043b..7c160f81e3e06 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -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() { @@ -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)]; + } } /** diff --git a/src/Symfony/Component/Validator/Tests/SampleBackedEnum.php b/src/Symfony/Component/Validator/Tests/SampleBackedEnum.php new file mode 100644 index 0000000000000..c40d1a44e00d9 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/SampleBackedEnum.php @@ -0,0 +1,18 @@ + + * + * 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'; +}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: