Skip to content

Conversation

@valx76
Copy link
Contributor

@valx76 valx76 commented Dec 16, 2025

Q A
Branch? 8.1
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

Add the ability to use enum in ChoiceQuestion.

TECHNICAL DETAILS

  • Works with all enum types (PR based on UnitEnum)
  • 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
{
    public function __invoke(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($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.

@valx76 valx76 requested a review from chalasr as a code owner December 16, 2025 13:20
@carsonbot carsonbot added this to the 8.1 milestone Dec 16, 2025
@valx76 valx76 changed the title [Console] Add the ability to use enums for ChoiceQuestion [Console] Add the ability to use enums for ChoiceQuestion (NOBUG) Dec 16, 2025
@valx76 valx76 changed the title [Console] Add the ability to use enums for ChoiceQuestion (NOBUG) [Console] Add the ability to use enums for ChoiceQuestion (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?

@OskarStark OskarStark changed the title [Console] Add the ability to use enums for ChoiceQuestion (NO-BUGFIX) [Console] Add the ability to use enums for ChoiceQuestion Dec 16, 2025
@valx76
Copy link
Contributor Author

valx76 commented Dec 17, 2025

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
{
    public function __invoke(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($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 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.

3 participants