[Form] Fix show float values as choices values in ChoiceType

This commit is contained in:
Yonel Ceruto 2016-11-01 16:33:50 -04:00
parent 620ea20f49
commit 35642288fa
2 changed files with 16 additions and 3 deletions

View File

@ -236,7 +236,11 @@ class ArrayChoiceList implements ChoiceListInterface
continue; continue;
} elseif (!is_scalar($choice)) { } elseif (!is_scalar($choice)) {
return false; return false;
} elseif (isset($cache[$choice])) { }
$choice = false === $choice ? '0' : (string) $choice;
if (isset($cache[$choice])) {
return false; return false;
} }

View File

@ -34,12 +34,12 @@ class ArrayChoiceListTest extends AbstractChoiceListTest
protected function getChoices() 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() 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 => true), $choiceList->getChoicesForValues(array('1')));
$this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0'))); $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')));
}
} }