From b8c60c739b0765743cb80327ad253afa0e757323 Mon Sep 17 00:00:00 2001 From: hliebe Date: Fri, 27 Oct 2017 22:26:54 +0200 Subject: [PATCH 1/3] [BUGFIX] ignore whitespaces in choicelist optionkeys --- .../Form/ChoiceList/ArrayKeyChoiceList.php | 6 ++++-- .../Tests/Extension/Core/Type/ChoiceTypeTest.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php b/src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php index 8710c968ee87d..5815cde9c8579 100644 --- a/src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php +++ b/src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php @@ -14,6 +14,7 @@ @trigger_error('The '.__NAMESPACE__.'\ArrayKeyChoiceList class is deprecated since version 2.8 and will be removed in 3.0. Use '.__NAMESPACE__.'\ArrayChoiceList instead.', E_USER_DEPRECATED); use Symfony\Component\Form\Exception\InvalidArgumentException; +use Symfony\Component\Form\Util\StringUtil; /** * A list of choices that can be stored in the keys of a PHP array. @@ -183,8 +184,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..04c69fe18204a 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,18 @@ 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' => array( + 'foobar ' => 'foobar ', + ) + )); + + $form->submit('foobar '); + + $this->assertEquals('foobar', $form->getData()); + } } From 4abe1758464bd5b9013d1cb753432c1122276306 Mon Sep 17 00:00:00 2001 From: hliebe Date: Fri, 27 Oct 2017 22:31:00 +0200 Subject: [PATCH 2/3] fabbot patch --- .../Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 04c69fe18204a..04e8476ee4de6 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -2484,7 +2484,7 @@ public function testSubmitSingleWhitspacedValue() 'multiple' => false, 'choices' => array( 'foobar ' => 'foobar ', - ) + ), )); $form->submit('foobar '); From 58119154ad7f921c1c97ba3927c954e1230d1a27 Mon Sep 17 00:00:00 2001 From: hliebe Date: Fri, 27 Oct 2017 22:45:45 +0200 Subject: [PATCH 3/3] removed deprecated call of choices_as_values false --- src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php | 6 ++++-- .../Component/Form/ChoiceList/ArrayKeyChoiceList.php | 6 ++---- .../Form/Tests/Extension/Core/Type/ChoiceTypeTest.php | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) 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/ChoiceList/ArrayKeyChoiceList.php b/src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php index 5815cde9c8579..8710c968ee87d 100644 --- a/src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php +++ b/src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php @@ -14,7 +14,6 @@ @trigger_error('The '.__NAMESPACE__.'\ArrayKeyChoiceList class is deprecated since version 2.8 and will be removed in 3.0. Use '.__NAMESPACE__.'\ArrayChoiceList instead.', E_USER_DEPRECATED); use Symfony\Component\Form\Exception\InvalidArgumentException; -use Symfony\Component\Form\Util\StringUtil; /** * A list of choices that can be stored in the keys of a PHP array. @@ -184,9 +183,8 @@ protected function flatten(array $choices, $value, &$choicesByValues, &$keysByVa } $choiceValue = (string) call_user_func($value, $choice); - $trimmedKey = StringUtil::trim($choiceValue); - $choicesByValues[$trimmedKey] = $choice; - $keysByValues[$trimmedKey] = $key; + $choicesByValues[$choiceValue] = $choice; + $keysByValues[$choiceValue] = $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 04e8476ee4de6..ce9b592615956 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -2482,6 +2482,7 @@ public function testSubmitSingleWhitspacedValue() { $form = $this->factory->create(static::TESTED_TYPE, null, array( 'multiple' => false, + 'choices_as_values' => true, 'choices' => array( 'foobar ' => 'foobar ', ), @@ -2489,6 +2490,6 @@ public function testSubmitSingleWhitspacedValue() $form->submit('foobar '); - $this->assertEquals('foobar', $form->getData()); + $this->assertEquals('foobar ', $form->getData()); } }