bug #21957 [Form] Choice type int values (BC Fix) (mcfedr)

This PR was squashed before being merged into the 2.7 branch (closes #21957).

Discussion
----------

[Form] Choice type int values (BC Fix)

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21952
| License       | MIT

#21267 unnecessarily forced all choice values to be strings, this was a BC braking change.

Everything can work fine if they are strings or ints. specifically the issue was because `array_flip` is used on the `choices` array, but this function will work fine with ints as well as strings.

Normally, when using HTML forms all data comes as strings, but the `symfony/form` component has use cases beyond this, specifically, I use it with JSON data, where valid values for a `ChoiceType` might not be strings.

Commits
-------

ed211e9c74 [Form] Choice type int values (BC Fix)
This commit is contained in:
Fabien Potencier 2017-03-10 11:10:40 -08:00
commit 641092fa16
2 changed files with 16 additions and 3 deletions

View File

@ -171,8 +171,8 @@ class ChoiceType extends AbstractType
}
foreach ($data as $v) {
if (null !== $v && !is_string($v)) {
throw new TransformationFailedException('All choices submitted must be NULL or strings.');
if (null !== $v && !is_string($v) && !is_int($v)) {
throw new TransformationFailedException('All choices submitted must be NULL, strings or ints.');
}
}
}, 256);

View File

@ -1689,6 +1689,19 @@ class ChoiceTypeTest extends BaseTypeTest
$this->assertNull($form[4]->getViewData());
}
public function testSubmitMultipleChoicesInts()
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'multiple' => true,
'choices' => array_flip($this->numericChoicesFlipped),
'choices_as_values' => true,
));
$form->submit(array(1, 2));
$this->assertTrue($form->isSynchronized());
}
public function testSingleSelectedObjectChoices()
{
$view = $this->factory->create(static::TESTED_TYPE, $this->objectChoices[3], array(
@ -2306,7 +2319,7 @@ class ChoiceTypeTest extends BaseTypeTest
$form->submit($submissionData);
$this->assertFalse($form->isSynchronized());
$this->assertEquals('All choices submitted must be NULL or strings.', $form->getTransformationFailure()->getMessage());
$this->assertEquals('All choices submitted must be NULL, strings or ints.', $form->getTransformationFailure()->getMessage());
}
public function invalidNestedValueTestMatrix()