8000 fix handling null as empty data · symfony/symfony@b5aa55d · GitHub
[go: up one dir, main page]

Skip to content

Commit b5aa55d

Browse files
committed
fix handling null as empty data
1 parent c6eb79a commit b5aa55d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ public function transform($value)
143143
*/
144144
public function reverseTransform($value)
145145
{
146-
if (!\is_string($value)) {
146+
if (null !== $value && !\is_string($value)) {
147147
throw new TransformationFailedException('Expected a string.');
148148
}
149149

150-
if ('' === $value) {
150+
if (null === $value || '' === $value) {
151151
return null;
152152
}
153153

src/Symfony/Component/Form/Tests/Extension/Core/Type/NumberTypeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedD
8585
$this->assertSame($expectedData, $form->getNormData());
8686
$this->assertSame($expectedData, $form->getData());
8787
}
88+
89+
public function testSubmitNullWithEmptyDataSetToNull()
90+
{
91+
$form = $this->factory->create(static::TESTED_TYPE, null, [
92+
'empty_data' => null,
93+
]);
94+
$form->submit(null);
95+
96+
$this->assertTrue($form->isSubmitted());
97+
$this->assertTrue($form->isSynchronized());
98+
$this->assertTrue($form->isValid());
99+
$this->assertSame('', $form->getViewData());
100+
$this->assertNull($form->getNormData());
101+
$this->assertNull($form->getData());
102+
}
88103
}

0 commit comments

Comments
 (0)
0