Skip to content

Commit 8967076

Browse files
committed
bug #17511 [Form] ArrayChoiceList can now deal with a null in choices (issei-m)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] ArrayChoiceList can now deal with a null in choices re-create for mistaken #17502 --- | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Documentation says, since 2.7, choice value is treated as corresponding item value (if `choices_as_values` option is true). Null as well. ```php $builder->add('attending', 'choice', array( 'choices' => array( 'yes' => true, 'no' => false, 'maybe' => null, ), 'choices_as_values' => true, )); ``` But actually null doesn't work as expected since `ArrayChoiceList::getChoicesForValues()` uses `isset` to check whether given value exists in its choices. It should use `array_key_exists` instead to do so here. Commits ------- 68292bb [Form] ArrayChoiceList can now deal with a null in choices
2 parents 1876b4f + 68292bb commit 8967076

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function getChoicesForValues(array $values)
141141
$choices = array();
142142

143143
foreach ($values as $i => $givenValue) {
144-
if (isset($this->choices[$givenValue])) {
144+
if (array_key_exists($givenValue, $this->choices)) {
145145
$choices[$i] = $this->choices[$givenValue];
146146
}
147147
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,11 @@ public function testCompareChoicesByIdentityByDefault()
130130
$this->assertSame(array(2 => 'value2'), $choiceList->getValuesForChoices(array(2 => $obj2)));
131131
$this->assertSame(array(2 => 'value2'), $choiceList->getValuesForChoices(array(2 => (object) array('value' => 'value2'))));
132132
}
133+
134+
public function testGetChoicesForValuesWithContainingNull()
135+
{
136+
$choiceList = new ArrayChoiceList(array('Null' => null));
137+
138+
$this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('0')));
139+
}
133140
}

0 commit comments

Comments
 (0)