Skip to content

Navigation Menu

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] 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

Closed
wants to merge 1 commit into from

Conversation

Ninos
Copy link
Contributor

@Ninos Ninos commented Jan 27, 2025

Q A
Branch? 7.3
Bug fix? no + very small
New feature? yes
Deprecations? no
License MIT

Feature

Supporting enum \class-string for choices attribute in Choice constraint.

Fix

  • ConstraintValidator->formatValue() returning correct value for enum type
  • Typo

Note

Using Choice constraint for attributes already defined as enum does not make sense (BackedEnumNormalizer already throws exception -> The data must belong to a backed enumeration of type App\\CustomEnum). It's only possible additionally using enum-classes instead of just arrays & callbacks.

Example

enum EnumTest: string
{
    case FirstCase = 'a';
    case SecondCase = 'b';
}

class SampleEntity {
    // Working
    #[Assert\Choice(choices: EnumTest::class)]
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $enum = EnumTest::FirstCase->value;

    // Not working
#    #[Assert\Choice(choices: EnumTest::class)]
#    #[ORM\Column(type: 'string', nullable: true, enumType: EnumTest::class)]
#    private ?EnumTest $enum = EnumTest::FirstCase;
}

@carsonbot carsonbot added this to the 7.3 milestone Jan 27, 2025
@Ninos Ninos force-pushed the feature/constraint-choice-enum branch 10 times, most recently from 8f76b52 to 23f36ab Compare January 28, 2025 02:18
@Ninos Ninos force-pushed the feature/constraint-choice-enum branch from 23f36ab to eca1cd7 Compare January 28, 2025 02:36
@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 BackedEnumNormalizer enums are currently (de)normalized from/to ->value, here it's ->name. Using some {{ value }} parameter in the messages block can confuse some, because input value is not same as displayed.

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 UnitEnumNormalizer, just BackedEnumNormalizer? If it makes sense to implement (for me it does), I'll create a new MR for that :-)

@@ -56,6 +56,7 @@ CHANGELOG
```
* Add support for ratio checks for SVG files to the `Image` constraint
* Add the `Slug` constraint
* Add support for enums in `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.

Enums are already supported

This PR add supports for using a class-string to specify the list of possible values ( integer or string )

@derrabus
Copy link
Member

Duplicate of #54226.

@derrabus derrabus closed this Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.