diff --git a/src/Symfony/Component/Console/Question/ChoiceQuestion.php b/src/Symfony/Component/Console/Question/ChoiceQuestion.php index 44b70abf11928..a4b302db3e18f 100644 --- a/src/Symfony/Component/Console/Question/ChoiceQuestion.php +++ b/src/Symfony/Component/Console/Question/ChoiceQuestion.php @@ -135,9 +135,15 @@ private function getDefaultValidator(): callable throw new InvalidArgumentException(sprintf($errorMessage, $selected)); } - $selectedChoices = array_map('trim', explode(',', $selected)); + $selectedChoices = explode(',', $selected); } else { - $selectedChoices = [trim($selected)]; + $selectedChoices = [$selected]; + } + + if ($this->isTrimmable()) { + foreach ($selectedChoices as $k => $v) { + $selectedChoices[$k] = trim($v); + } } $multiselectChoices = []; diff --git a/src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php b/src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php index 5ec7a9cacb4de..9db12f8528412 100644 --- a/src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php +++ b/src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php @@ -61,4 +61,20 @@ public function selectUseCases() ], ]; } + + public function testNonTrimmable() + { + $question = new ChoiceQuestion('A question', [ + 'First response ', + ' Second response', + ' Third response ', + ]); + $question->setTrimmable(false); + + $this->assertSame(' Third response ', $question->getValidator()(' Third response ')); + + $question->setMultiselect(true); + + $this->assertSame(['First response ', ' Second response'], $question->getValidator()('First response , Second response')); + } }