Skip to content

Commit b300b60

Browse files
bug symfony#57920 [Form] Fix handling empty data in ValueToDuplicatesTransformer (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Form] Fix handling empty data in ValueToDuplicatesTransformer | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#47013 | License | MIT The transformer receives the data of child forms that have already been through the transformation schema. If no custom view transformer was used on that child form the empty would already have been changed to null. Thus, receiving an empty string here means that the child form explicitly asked for it and the value must not be exchanged with null. Commits ------- 4ec6c80 fix handling empty data in ValueToDuplicatesTransformer
2 parents 1a7fa93 + 4ec6c80 commit b300b60

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function reverseTransform($array)
6262
$emptyKeys = [];
6363

6464
foreach ($this->keys as $key) {
65-
if (isset($array[$key]) && '' !== $array[$key] && false !== $array[$key] && [] !== $array[$key]) {
65+
if (isset($array[$key]) && false !== $array[$key] && [] !== $array[$key]) {
6666
if ($array[$key] !== $result) {
6767
throw new TransformationFailedException('All values in the array should be the same.');
6868
}

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testReverseTransformCompletelyEmpty()
7070
'c' => '',
7171
];
7272

73-
$this->assertNull($this->transformer->reverseTransform($input));
73+
$this->assertSame('', $this->transformer->reverseTransform($input));
7474
}
7575

7676
public function testReverseTransformCompletelyNull()

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

14+
use Symfony\Component\Form\Extension\Core\Type\TextType;
1415
use Symfony\Component\Form\Form;
1516
use Symfony\Component\Form\Tests\Fixtures\NotMappedType;
1617
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
@@ -191,6 +192,36 @@ public function testSetOptionsPerChildAndOverwrite()
191192
$this->assertTrue($form['second']->isRequired());
192193
}
193194

195+
/**
196+
* @dataProvider emptyDataProvider
197+
*/
198+
public function testSubmitNullForTextTypeWithEmptyDataOptionSetToEmptyString($emptyData, $submittedData, $expected)
199+
{
200+
$form = $this->factory->create(static::TESTED_TYPE, null, [
201+
'type' => TextType::class,
202+
'options' => [
203+
'empty_data' => $emptyData,
204+
]
205+
]);
206+
$form->submit($submittedData);
207+
208+
$this->assertSame($expected, $form->getData());
209+
}
210+
211+
public static function emptyDataProvider()
212+
{
213+
yield ['', null, ''];
214+
yield ['', ['first' => null, 'second' => null], ''];
215+
yield ['', ['first' => '', 'second' => null], ''];
216+
yield ['', ['first' => null, 'second' => ''], ''];
217+
yield ['', ['first' => '', 'second' => ''], ''];
218+
yield [null, null, null];
219+
yield [null, ['first' => null, 'second' => null], null];
220+
yield [null, ['first' => '', 'second' => null], null];
221+
yield [null, ['first' => null, 'second' => ''], null];
222+
yield [null, ['first' => '', 'second' => ''], null];
223+
}
224+
194225
public function testSubmitUnequal()
195226
{
196227
$input = ['first' => 'foo', 'second' => 'bar'];

0 commit comments

Comments
 (0)