bug #37505 [Form] fix handling null as empty data (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] fix handling null as empty data

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37493
| License       | MIT
| Doc PR        |

Commits
-------

b5aa55dab9 fix handling null as empty data
This commit is contained in:
Nicolas Grekas 2020-07-06 15:09:26 +02:00
commit 091fd5016d
2 changed files with 17 additions and 2 deletions

View File

@ -143,11 +143,11 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
*/
public function reverseTransform($value)
{
if (!\is_string($value)) {
if (null !== $value && !\is_string($value)) {
throw new TransformationFailedException('Expected a string.');
}
if ('' === $value) {
if (null === $value || '' === $value) {
return null;
}

View File

@ -85,4 +85,19 @@ class NumberTypeTest extends BaseTypeTest
$this->assertSame($expectedData, $form->getNormData());
$this->assertSame($expectedData, $form->getData());
}
public function testSubmitNullWithEmptyDataSetToNull()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'empty_data' => null,
]);
$form->submit(null);
$this->assertTrue($form->isSubmitted());
$this->assertTrue($form->isSynchronized());
$this->assertTrue($form->isValid());
$this->assertSame('', $form->getViewData());
$this->assertNull($form->getNormData());
$this->assertNull($form->getData());
}
}