Skip to content

Commit

Permalink
bug #17511 [Form] ArrayChoiceList can now deal with a null in choices…
Browse files Browse the repository at this point in the history
… (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
  • Loading branch information
fabpot committed Jan 27, 2016
2 parents 1876b4f + 68292bb commit 8967076
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php
Expand Up @@ -141,7 +141,7 @@ public function getChoicesForValues(array $values)
$choices = array();

foreach ($values as $i => $givenValue) {
if (isset($this->choices[$givenValue])) {
if (array_key_exists($givenValue, $this->choices)) {
$choices[$i] = $this->choices[$givenValue];
}
}
Expand Down
Expand Up @@ -130,4 +130,11 @@ public function testCompareChoicesByIdentityByDefault()
$this->assertSame(array(2 => 'value2'), $choiceList->getValuesForChoices(array(2 => $obj2)));
$this->assertSame(array(2 => 'value2'), $choiceList->getValuesForChoices(array(2 => (object) array('value' => 'value2'))));
}

public function testGetChoicesForValuesWithContainingNull()
{
$choiceList = new ArrayChoiceList(array('Null' => null));

$this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('0')));
}
}

0 comments on commit 8967076

Please sign in to comment.