-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Closed
Description
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