Skip to content

Commit

Permalink
bug #57920 [Form] Fix handling empty data in ValueToDuplicatesTransfo…
Browse files Browse the repository at this point in the history
…rmer (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 #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
-------

4ec6c800b9 fix handling empty data in ValueToDuplicatesTransformer
  • Loading branch information
nicolas-grekas committed Aug 8, 2024
2 parents dc6e748 + 02f1d9f commit ea981b0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function reverseTransform($array)
$emptyKeys = [];

foreach ($this->keys as $key) {
if (isset($array[$key]) && '' !== $array[$key] && false !== $array[$key] && [] !== $array[$key]) {
if (isset($array[$key]) && false !== $array[$key] && [] !== $array[$key]) {
if ($array[$key] !== $result) {
throw new TransformationFailedException('All values in the array should be the same.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testReverseTransformCompletelyEmpty()
'c' => '',
];

$this->assertNull($this->transformer->reverseTransform($input));
$this->assertSame('', $this->transformer->reverseTransform($input));
}

public function testReverseTransformCompletelyNull()
Expand Down
31 changes: 31 additions & 0 deletions Tests/Extension/Core/Type/RepeatedTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

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

/**
* @dataProvider emptyDataProvider
*/
public function testSubmitNullForTextTypeWithEmptyDataOptionSetToEmptyString($emptyData, $submittedData, $expected)
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'type' => TextType::class,
'options' => [
'empty_data' => $emptyData,
]
]);
$form->submit($submittedData);

$this->assertSame($expected, $form->getData());
}

public static function emptyDataProvider()
{
yield ['', null, ''];
yield ['', ['first' => null, 'second' => null], ''];
yield ['', ['first' => '', 'second' => null], ''];
yield ['', ['first' => null, 'second' => ''], ''];
yield ['', ['first' => '', 'second' => ''], ''];
yield [null, null, null];
yield [null, ['first' => null, 'second' => null], null];
yield [null, ['first' => '', 'second' => null], null];
yield [null, ['first' => null, 'second' => ''], null];
yield [null, ['first' => '', 'second' => ''], null];
}

public function testSubmitUnequal()
{
$input = ['first' => 'foo', 'second' => 'bar'];
Expand Down

0 comments on commit ea981b0

Please sign in to comment.