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

[Console] Add the ability to use enums forChoiceQuestion#62784

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

Open
valx76 wants to merge4 commits intosymfony:8.1
base:8.1
Choose a base branch
Loading
fromvalx76:choice-question-accept-enums

Conversation

@valx76
Copy link
Contributor

@valx76valx76 commentedDec 16, 2025
edited
Loading

QA
Branch?8.1
Bug fix?no
New feature?yes
Deprecations?no
Issues
LicenseMIT

Add the ability to use enum inChoiceQuestion.

TECHNICAL DETAILS

  • Works with all enum types (PR based onUnitEnum)
  • Uses enum case names for display (for example, it converts "MyEnumValue" to "My enum value")
  • Enum cases can be selected by their index or name (names can be autocompleted)
  • The multiselect and multiline options are disabled when using enums and trying to enable them will trigger an exception
  • The default value of an enum choice question needs to be a value from the enum otherwise it will trigger an exception

Warning

Right now, users need to follow the enum cases’ naming convention written in the Symfony documentation for the feature to work.
We can maybe allow the user inject a callable with a different naming strategy so users can choose another naming convention?
Updated with comment#62784 (comment)

USAGE

enum ColorEnum{case ColorRed;case ColorGreen;case Blue;}#[AsCommand(name:'app:run')]class MyConsole{publicfunction__invoke(InputInterface$input,OutputInterface$output):int    {$io =newSymfonyStyle($input,$output);$color =$io->choice('Please choose a color:',            ColorEnum::class,            ColorEnum::Blue        );dump($color);return Command::SUCCESS;    }}
ScreenSymfony

In the screenshot above, the first run is using the enum case' index and the second one is using its name.

@carsonbotcarsonbot added this to the8.1 milestoneDec 16, 2025
@valx76valx76 changed the title[Console] Add the ability to use enums forChoiceQuestion[Console] Add the ability to use enums forChoiceQuestion (NOBUG)Dec 16, 2025
@valx76valx76 changed the title[Console] Add the ability to use enums forChoiceQuestion (NOBUG)[Console] Add the ability to use enums forChoiceQuestion (NO-BUGFIX)Dec 16, 2025
@derrabus
Copy link
Member

How do I control how enum cases are rendered, e.g. if I want to translate them?

@OskarStarkOskarStark changed the title[Console] Add the ability to use enums forChoiceQuestion (NO-BUGFIX)[Console] Add the ability to use enums forChoiceQuestionDec 16, 2025
@valx76
Copy link
ContributorAuthor

How do I control how enum cases are rendered, e.g. if I want to translate them?

@derrabus I've updated the PR so we can now inject a callable to customize enum cases' rendering:

enum ColorEnum{case ColorRed;case ColorGreen;case Blue;}#[AsCommand(name:'app:run')]class MyConsole{publicfunction__invoke(InputInterface$input,OutputInterface$output):int    {$io =newSymfonyStyle($input,$output);$color =$io->choice('Please choose a color:',            ColorEnum::class,            ColorEnum::Blue,            customEnumRender:fn (ColorEnum$color) =>match ($color) {                ColorEnum::ColorRed =>'My red color',                ColorEnum::ColorGreen =>'Custom green color',                ColorEnum::Blue =>'THE Blue',            }        );dump($color);return Command::SUCCESS;    }}

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

Reviewers

@chalasrchalasrAwaiting requested review from chalasrchalasr is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

8.1

Development

Successfully merging this pull request may close these issues.

3 participants

@valx76@derrabus@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp