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 7f2824fab216a..4a2588ef1d475 100644 --- a/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php +++ b/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php @@ -236,7 +236,11 @@ private function castableToString(array $choices, array &$cache = array()) 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 09bcc9c4c2aa1..3b041eab293d1 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/ArrayChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/ArrayChoiceListTest.php @@ -34,12 +34,12 @@ protected function createChoiceList() 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 @@ public function testGetChoicesForValuesWithContainingEmptyStringAndBooleans() $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'))); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy