Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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 1 commit
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
Next Next commit
feat: support enums in Choice constraints
  • Loading branch information
bpolaszek committed Sep 16, 2021
commit 1f9a3a115890b1436e6b228793e29a92bf7e5e5e
1 change: 1 addition & 0 deletions 1 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,7 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, Choice::class);
}

if (!\is_array($constraint->choices) && !$constraint->callback) {
if (!\is_array($constraint->choices) && !is_a($constraint->choices, \BackedEnum::class, true) && !$constraint->callback) {
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice.');
}

Expand All @@ -55,6 +55,13 @@ 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_map(
function (\BackedEnum $enum) {
return $enum->value;
bpolaszek marked this conversation as resolved.
Show resolved Hide resolved
},
$constraint->choices::cases()
bpolaszek marked this conversation as resolved.
Show resolved Hide resolved
);
} else {
$choices = $constraint->choices;
}
Expand Down
20 changes: 20 additions & 0 deletions 20 src/Symfony/Component/Validator/Test/SampleBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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.
*/

declare(strict_types=1);
bpolaszek marked this conversation as resolved.
Show resolved Hide resolved

namespace Symfony\Component\Validator\Test;
bpolaszek marked this conversation as resolved.
Show resolved Hide resolved

enum SampleBackedEnum: string
{
case FOO = 'foo';
case BAR = 'bar';
}
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\Test\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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.