Skip to content

Commit 3564228

Browse files
committed
[Form] Fix show float values as choices values in ChoiceType
1 parent 620ea20 commit 3564228

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ private function castableToString(array $choices, array &$cache = array())
236236
continue;
237237
} elseif (!is_scalar($choice)) {
238238
return false;
239-
} elseif (isset($cache[$choice])) {
239+
}
240+
241+
$choice = false === $choice ? '0' : (string) $choice;
242+
243+
if (isset($cache[$choice])) {
240244
return false;
241245
}
242246

src/Symfony/Component/Form/Tests/ChoiceList/ArrayChoiceListTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ protected function createChoiceList()
3434

3535
protected function getChoices()
3636
{
37-
return array(0, 1, '1', 'a', false, true, $this->object, null);
37+
return array(0, 1, 1.5, '1', 'a', false, true, $this->object, null);
3838
}
3939

4040
protected function getValues()
4141
{
42-
return array('0', '1', '2', '3', '4', '5', '6', '7');
42+
return array('0', '1', '2', '3', '4', '5', '6', '7', '8');
4343
}
4444

4545
/**
@@ -162,4 +162,13 @@ public function testGetChoicesForValuesWithContainingEmptyStringAndBooleans()
162162
$this->assertSame(array(0 => true), $choiceList->getChoicesForValues(array('1')));
163163
$this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0')));
164164
}
165+
166+
public function testGetChoicesForValuesWithContainingEmptyStringAndFloats()
167+
{
168+
$choiceList = new ArrayChoiceList(array('Empty String' => '', '1/3' => 0.3, '1/2' => 0.5));
169+
170+
$this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array('')));
171+
$this->assertSame(array(0 => 0.3), $choiceList->getChoicesForValues(array('0.3')));
172+
$this->assertSame(array(0 => 0.5), $choiceList->getChoicesForValues(array('0.5')));
173+
}
165174
}

0 commit comments

Comments
 (0)
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