From 35642288faebec8b5d8ec9028494b43b78348a69 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 1 Nov 2016 16:33:50 -0400 Subject: [PATCH] [Form] Fix show float values as choices values in ChoiceType --- .../Component/Form/ChoiceList/ArrayChoiceList.php | 6 +++++- .../Form/Tests/ChoiceList/ArrayChoiceListTest.php | 13 +++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php b/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php index 7f2824fab2..4a2588ef1d 100644 --- a/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php +++ b/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php @@ -236,7 +236,11 @@ class ArrayChoiceList implements ChoiceListInterface continue; } elseif (!is_scalar($choice)) { return false; - } elseif (isset($cache[$choice])) { + } + + $choice = false === $choice ? '0' : (string) $choice; + + if (isset($cache[$choice])) { return false; } diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/ArrayChoiceListTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/ArrayChoiceListTest.php index 09bcc9c4c2..3b041eab29 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/ArrayChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/ArrayChoiceListTest.php @@ -34,12 +34,12 @@ class ArrayChoiceListTest extends AbstractChoiceListTest protected function getChoices() { - return array(0, 1, '1', 'a', false, true, $this->object, null); + return array(0, 1, 1.5, '1', 'a', false, true, $this->object, null); } protected function getValues() { - return array('0', '1', '2', '3', '4', '5', '6', '7'); + return array('0', '1', '2', '3', '4', '5', '6', '7', '8'); } /** @@ -162,4 +162,13 @@ class ArrayChoiceListTest extends AbstractChoiceListTest $this->assertSame(array(0 => true), $choiceList->getChoicesForValues(array('1'))); $this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0'))); } + + public function testGetChoicesForValuesWithContainingEmptyStringAndFloats() + { + $choiceList = new ArrayChoiceList(array('Empty String' => '', '1/3' => 0.3, '1/2' => 0.5)); + + $this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array(''))); + $this->assertSame(array(0 => 0.3), $choiceList->getChoicesForValues(array('0.3'))); + $this->assertSame(array(0 => 0.5), $choiceList->getChoicesForValues(array('0.5'))); + } }