Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 091fd50

Browse filesBrowse files
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 ------- b5aa55d fix handling null as empty data
2 parents 4297897 + b5aa55d commit 091fd50
Copy full SHA for 091fd50

File tree

Expand file treeCollapse file tree

2 files changed

+17
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/NumberTypeTest.php
+15Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.