Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[Validator] Add support for enum inChoice 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

Ninos
Copy link
Contributor

@NinosNinos commentedJan 27, 2025
edited
Loading

QA
Branch?7.3
Bug fix?no + very small
New feature?yes
Deprecations?no
LicenseMIT

Feature

Supporting enum\class-string forchoices attribute inChoice constraint.

Fix

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

Note

UsingChoice 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;}

@carsonbotcarsonbot added this to the7.3 milestoneJan 27, 2025
@NinosNinosforce-pushed thefeature/constraint-choice-enum branch 10 times, most recently from8f76b52 to23f36abCompareJanuary 28, 2025 02:18
@NinosNinosforce-pushed thefeature/constraint-choice-enum branch from23f36ab toeca1cd7CompareJanuary 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
ContributorAuthor

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 theBackedEnumNormalizer 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 noUnitEnumNormalizer, justBackedEnumNormalizer? 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 )

Ninos reacted with thumbs up emoji
@derrabus
Copy link
Member

Duplicate of#54226.

Ninos reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@smnandresmnandresmnandre left review comments

Assignees
No one assigned
Projects
None yet
Milestone
7.3
Development

Successfully merging this pull request may close these issues.

4 participants
@Ninos@derrabus@smnandre@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp