8000 [Form] Fixed ChoiceField::isChoiceSelected() to differentiate between… · wowo/symfony@40acc6a · GitHub
[go: up one dir, main page]

Skip to content

Commit 40acc6a

Browse files
Bernhard Schussekfabpot
authored andcommitted
[Form] Fixed ChoiceField::isChoiceSelected() to differentiate between zero and empty
1 parent 1593d6f commit 40acc6a

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

src/Symfony/Component/Form/ChoiceField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function isChoiceGroup($choice)
180180

181181
public function isChoiceSelected($choice)
182182
{
183-
return in_array($choice, (array) $this->getDisplayedData());
183+
return in_array((string) $choice, (array) $this->getDisplayedData(), true);
184184
}
185185

186186
public function isMultipleChoice()

src/Symfony/Component/Form/Field.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,9 @@ protected function denormalize($value)
494494
protected function transform($value)
495495
{
496496
if (null === $this->valueTransformer) {
497-
return null === $value ? '' : $value;
497+
// Scalar values should always be converted to strings to
498+
// facilitate differentiation between empty ("") and zero (0).
499+
return null === $value || is_scalar($value) ? (string)$value : $value;
498500
}
499501
return $this->valueTransformer->transform($value);
500502
}

tests/Symfony/Tests/Component/Form/ChoiceFieldTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,62 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
4646
4 => 'Roman',
4747
);
4848

49+
public function testIsChoiceSelectedDifferentiatesBetweenZeroAndEmpty_integerZero()
50+
{
51+
$field = new ChoiceField('name', array(
52+
'choices' => array(
53+
0 => 'Foo',
54+
'' => 'Bar',
55+
)
56+
));
57+
58+
$field->submit(0);
59+
60+
$this->assertTrue($field->isChoiceSelected(0));
61+
$this->assertTrue($field->isChoiceSelected('0'));
62+
$this->assertFalse($field->isChoiceSelected(''));
63+
64+
$field->submit('0');
65+
66+
$this->assertTrue($field->isChoiceSelected(0));
67+
$this->assertTrue($field->isChoiceSelected('0'));
68+
$this->assertFalse($field->isChoiceSelected(''));
69+
70+
$field->submit('');
71+
72+
$this->assertFalse($field->isChoiceSelected(0));
73+
$this->assertFalse($field->isChoiceSelected('0'));
74+
$this->assertTrue($field->isChoiceSelected(''));
75+
}
76+
77+
public function testIsChoiceSelectedDifferentiatesBetweenZeroAndEmpty_stringZero()
78+
{
79+
$field = new ChoiceField('name', array(
80+
'choices' => array(
81+
'0' => 'Foo',
82+
'' => 'Bar',
83+
)
84+
));
85+
86+
$field->submit(0);
87+
88+
$this->assertTrue($field->isChoiceSelected(0));
89+
$this->assertTrue($field->isChoiceSelected('0'));
90+
$this->assertFalse($field->isChoiceSelected(''));
91+
92+
$field->submit('0');
93+
94+
$this->assertTrue($field->isChoiceSelected(0));
95+
$this->assertTrue($field->isChoiceSelected('0'));
96+
$this->assertFalse($field->isChoiceSelected(''));
97+
98+
$field->submit('');
99+
100+
$this->assertFalse($field->isChoiceSelected(0));
101+
$this->assertFalse($field->isChoiceSelected('0'));
102+
$this->assertTrue($field->isChoiceSelected(''));
103+
}
104+
49105
/**
50106
* @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
51107
*/

tests/Symfony/Tests/Component/Form/FieldTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
220220
$this->assertSame('', $this->field->getDisplayedData());
221221
}
222222

223+
public function testValuesAreTransformedCorrectlyIfNotNull_noValueTransformer()
224+
{
225+
$this->field->setData(123);
226+
227+
$this->assertSame(123, $this->field->getData());
228+
$this->assertSame('123', $this->field->getDisplayedData());
229+
}
230+
223231
public function testSubmittedValuesAreTransformedCorrectly()
224232
{
225233
$valueTransformer = $this->createMockTransformer();

0 commit comments

Comments
 (0)
0