Skip to content

Commit b769b77

Browse files
bug symfony#52255 [Form] Skip merging params & files if there are no files in the first place (dmaicher, priyadi)
This PR was merged into the 5.4 branch. Discussion ---------- [Form] Skip merging params & files if there are no files in the first place | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#52248 | License | MIT Skip merging params & files if there are no files in the first place. Works around problems with numeric field names. Commits ------- 889c23e [Form] Skip merging params & files if there are no files in the first place
2 parents 080b32f + 889c23e commit b769b77

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/Symfony/Component/Form/Tests/AbstractRequestHandlerTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
17+
use Symfony\Component\Form\Extension\Core\Type\TextType;
1718
use Symfony\Component\Form\Form;
1819
use Symfony\Component\Form\FormBuilder;
1920
use Symfony\Component\Form\FormError;
@@ -236,6 +237,24 @@ public function testMergeParamsAndFiles($method)
236237
$this->assertSame($file, $form->get('field2')->getData());
237238
}
238239

240+
public function testIntegerChildren()
241+
{
242+
$form = $this->createForm('root', 'POST', true);
243+
$form->add('0', TextType::class);
244+
$form->add('1', TextType::class);
245+
246+
$this->setRequestData('POST', [
247+
'root' => [
248+
'1' => 'bar',
249+
],
250+
]);
251+
252+
$this->requestHandler->handleRequest($form, $this->request);
253+
254+
$this->assertNull($form->get('0')->getData());
255+
$this->assertSame('bar', $form->get('1')->getData());
256+
}
257+
239258
/**
240259
* @dataProvider methodExceptGetProvider
241260
*/

src/Symfony/Component/Form/Util/FormUtil.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,21 @@ public static function isEmpty($data)
5050
*/
5151
public static function mergeParamsAndFiles(array $params, array $files): array
5252
{
53-
$result = [];
53+
if (array_is_list($files)) {
54+
foreach ($files as $value) {
55+
$params[] = $value;
56+
}
57+
58+
return $params;
59+
}
5460

5561
foreach ($params as $key => $value) {
5662
if (\is_array($value) && \is_array($files[$key] ?? null)) {
57-
$value = self::mergeParamsAndFiles($value, $files[$key]);
63+
$params[$key] = self::mergeParamsAndFiles($value, $files[$key]);
5864
unset($files[$key]);
5965
}
60-
if (\is_int($key)) {
61-
$result[] = $value;
62-
} else {
63-
$result[$key] = $value;
64-
}
6566
}
6667

67-
return array_merge($result, $files);
68+
return array_replace($params, $files);
6869
}
6970
}

0 commit comments

Comments
 (0)