-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add support for enum in Choice
constraint
#59633
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,7 @@ protected function formatValue(mixed $value, int $format = 0): string | |
} | ||
|
||
if ($value instanceof \UnitEnum) { | ||
return $value->name; | ||
$value = $value instanceof \BackedEnum ? $value->value : $value->name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally don't think this is a good idea. Enum case names are helpful for developers, scalar equivalent less so. Moreover, this will have an impact on other constraints, right ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I don't need this enum functionality in choice constraint (just had an idea & started coding), but this line of code should be fixed upstream. In the Btw this should not break anything, may third-party tests depending on this functionality. Haven't seen much enum usages in constraints and tests worked smooth for the other part of code. PS: Is there any reason why there's no |
||
} | ||
|
||
if (\is_object($value)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
* Validates that a value is one of a given set of valid choices. | ||
* | ||
* @author Bernhard Schussek <[email protected]> | ||
* @author Ninos Ego <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class Choice extends Constraint | ||
|
@@ -32,7 +33,8 @@ class Choice extends Constraint | |
self::TOO_MANY_ERROR => 'TOO_MANY_ERROR', | ||
]; | ||
|
||
public ?array $choices = null; | ||
/** @var \class-string|array|null */ | ||
public string|array|null $choices = null; | ||
/** @var callable|string|null */ | ||
public $callback; | ||
public bool $multiple = false; | ||
|
@@ -45,25 +47,27 @@ class Choice extends Constraint | |
public string $maxMessage = 'You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.'; | ||
public bool $match = true; | ||
|
||
public ?\Closure $normalizer; | ||
|
||
public function getDefaultOption(): ?string | ||
{ | ||
return 'choices'; | ||
} | ||
|
||
/** | ||
* @param array|null $choices An array of choices (required unless a callback is specified) | ||
* @param callable|string|null $callback Callback method to use instead of the choice option to get the choices | ||
* @param bool|null $multiple Whether to expect the value to be an array of valid choices (defaults to false) | ||
* @param bool|null $strict This option defaults to true and should not be used | ||
* @param int<0, max>|null $min Minimum of valid choices if multiple values are expected | ||
* @param positive-int|null $max Maximum of valid choices if multiple values are expected | ||
* @param string[]|null $groups | ||
* @param bool|null $match Whether to validate the values are part of the choices or not (defaults to true) | ||
* @param \class-string|array|null $choices An enum or array of choices (required unless a callback is specified) | ||
* @param callable|string|null $callback Callback method to use instead of the choice option to get the choices | ||
* @param bool|null $multiple Whether to expect the value to be an array of valid choices (defaults to false) | ||
* @param bool|null $strict This option defaults to true and should not be used | ||
* @param int<0, max>|null $min Minimum of valid choices if multiple values are expected | ||
* @param positive-int|null $max Maximum of valid choices if multiple values are expected | ||
* @param string[]|null $groups | ||
* @param bool|null $match Whether to validate the values are part of the choices or not (defaults to true) | ||
*/ | ||
#[HasNamedArguments] | ||
public function __construct( | ||
string|array $options = [], | ||
?array $choices = null, | ||
string|array|null $choices = null, | ||
callable|string|null $callback = null, | ||
?bool $multiple = null, | ||
?bool $strict = null, | ||
|
@@ -73,11 +77,12 @@ public function __construct( | |
?string $multipleMessage = null, | ||
?string $minMessage = null, | ||
?string $maxMessage = null, | ||
?callable $normalizer = null, | ||
?array $groups = null, | ||
mixed $payload = null, | ||
?bool $match = null, | ||
) { | ||
if (\is_array($options) && $options && array_is_list($options)) { | ||
if (\is_array($options) && $options && \array_is_list($options)) { | ||
$choices ??= $options; | ||
$options = []; | ||
} elseif (\is_array($options) && [] !== $options) { | ||
|
@@ -100,5 +105,6 @@ public function __construct( | |
$this->minMessage = $minMessage ?? $this->minMessage; | ||
$this->maxMessage = $maxMessage ?? $this->maxMessage; | ||
$this->match = $match ?? $this->match; | ||
$this->normalizer = null !== $normalizer ? $normalizer(...) : null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
* @author Fabien Potencier <[email protected]> | ||
* @author Florian Eckerstorfer <[email protected]> | ||
* @author Bernhard Schussek <[email protected]> | ||
* @author Ninos Ego <[email protected]> | ||
*/ | ||
class ChoiceValidator extends ConstraintValidator | ||
{ | ||
|
@@ -33,16 +34,24 @@ public function validate(mixed $value, Constraint $constraint): void | |
throw new UnexpectedTypeException($constraint, Choice::class); | ||
} | ||
|
||
if (!\is_array($constraint->choices) && !$constraint->callback) { | ||
if (null === $constraint->choices && !$constraint->callback) { | ||
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice.'); | ||
} | ||
|
||
if (null !== $constraint->choices && !\is_array($constraint->choices) && (!\is_string($constraint->choices) || !\enum_exists($constraint->choices))) { | ||
throw new ConstraintDefinitionException('"choices" must be of type array or enum-class.'); | ||
} | ||
|
||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if ($constraint->multiple && !\is_array($value)) { | ||
throw new UnexpectedValueException($value, 'array'); | ||
if (null !== $constraint->normalizer) { | ||
$value = ($constraint->normalizer)($value); | ||
} | ||
|
||
if ($constraint->multiple && !\is_array($value) && !$value instanceof \IteratorAggregate) { | ||
throw new UnexpectedValueException($value, 'array|IteratorAggregate'); | ||
} | ||
|
||
if ($constraint->callback) { | ||
|
@@ -56,6 +65,10 @@ public function validate(mixed $value, Constraint $constraint): void | |
if (!\is_array($choices)) { | ||
throw new ConstraintDefinitionException(\sprintf('The Choice constraint callback "%s" is expected to return an array, but returned "%s".', trim($this->formatValue($constraint->callback), '"'), get_debug_type($choices))); | ||
} | ||
} elseif (\is_string($constraint->choices) && \enum_exists($constraint->choices)) { | ||
$choices = \array_map(static function(\UnitEnum $value): string|int { | ||
return $value instanceof \BackedEnum ? $value->value : $value->name; | ||
}, $constraint->choices::cases()); | ||
} else { | ||
$choices = $constraint->choices; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Fixtures; | ||
|
||
/** | ||
* @author Ninos Ego <[email protected]> | ||
*/ | ||
enum TestEnumBackendInteger: int | ||
{ | ||
case FirstCase = 3; | ||
case SecondCase = 4; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enums are already supported
This PR add supports for using a class-string to specify the list of possible values ( integer or string )