diff --git a/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php b/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php index 676987e641216..6823d3a50f399 100644 --- a/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php +++ b/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Form\ChoiceList; use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\Util\StringUtil; /** * A list of choices with arbitrary data types. @@ -209,8 +210,9 @@ protected function flatten(array $choices, $value, &$choicesByValues, &$keysByVa } $choiceValue = (string) call_user_func($value, $choice); - $choicesByValues[$choiceValue] = $choice; - $keysByValues[$choiceValue] = $key; + $trimmedKey = StringUtil::trim($choiceValue); + $choicesByValues[$trimmedKey] = $choice; + $keysByValues[$trimmedKey] = $key; $structuredValues[$key] = $choiceValue; } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index 618a1bada7edb..ce9b592615956 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -2477,4 +2477,19 @@ public function testStripLeadingUnderscoresAndDigitsFromId() $this->assertEquals('_09name', $view->vars['name']); $this->assertEquals('_09name', $view->vars['full_name']); } + + public function testSubmitSingleWhitspacedValue() + { + $form = $this->factory->create(static::TESTED_TYPE, null, array( + 'multiple' => false, + 'choices_as_values' => true, + 'choices' => array( + 'foobar ' => 'foobar ', + ), + )); + + $form->submit('foobar '); + + $this->assertEquals('foobar ', $form->getData()); + } }