Skip to content

Console choice forces answers as strings #34789

@LordZardeck

Description

@LordZardeck

Symfony version(s) affected: 4.3

Description
When using choice, selected answers are forced into strings, preventing us from using complex values such as a class with a custom __toString. This is a problem, as I need the ability to present the user with a list of display strings to choose from, but need the ID associated with that display string in order to do anything useful.

How to reproduce

class UserRoleOption
{

    public $id;
    public $name;

    public function __construct($id, $name)
    {
        $this->id = $id;
        $this->name = $name;
    }

    public function __toString()
    {
        return $this->name;
    }
}

foreach ($rolesQuery as $role) {
    $roles[] = new UserRoleOption($role->id, $role->name);
}

var_dump($this->choice('Select roles to assign', $roles));

Possible Solution
Remove the cast to string on this line: https://github.com/symfony/console/blob/master/Question/ChoiceQuestion.php#L172

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy