Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2744144

Browse files
committed
bug #52347 [Form] Fix merging form data and files (ter) (Jan Pintr)
This PR was merged into the 5.4 branch. Discussion ---------- [Form] Fix merging form data and files (ter) | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #52318 | License | MIT Adapted from a patch provided by `@jan`-pintr The original was like below but this one looks more appropriate to me. <details> ```diff --- a/src/Symfony/Component/Form/Util/FormUtil.php +++ b/src/Symfony/Component/Form/Util/FormUtil.php @@ -47,8 +47,12 @@ public static function isEmpty(mixed $data): bool public static function mergeParamsAndFiles(array $params, array $files): array { if (array_is_list($files)) { - foreach ($files as $value) { - $params[] = $value; + foreach ($files as $key => $value) { + if (is_array($params[$key])) { + $params[$key] = array_merge($params[$key], $value); + } else { + $params[] = $value; + } } return $params; @@ -61,6 +65,6 @@ public static function mergeParamsAndFiles(array $params, array $files): array } } - return array_replace($params, $files); + return array_merge($params, $files); }``` </details> Commits ------- 9facb2f [Form] Fix merging form data and files (ter)
2 parents 4a41159 + 9facb2f commit 2744144

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

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

Lines changed: 44 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\CollectionType;
1718
use Symfony\Component\Form\Extension\Core\Type\TextType;
1819
use Symfony\Component\Form\Form;
1920
use Symfony\Component\Form\FormBuilder;
@@ -23,6 +24,7 @@
2324
use Symfony\Component\Form\Forms;
2425
use Symfony\Component\Form\RequestHandlerInterface;
2526
use Symfony\Component\Form\ResolvedFormTypeFactory;
27+
use Symfony\Component\Form\Tests\Extension\Type\ItemFileType;
2628
use Symfony\Component\Form\Util\ServerParams;
2729

2830
/**
@@ -311,6 +313,48 @@ public function testParamTakesPrecedenceOverFile($method)
311313
$this->assertSame('DATA', $form->getData());
312314
}
313315

316+
public function testMergeZeroIndexedCollection()
317+
{
318+
$form = $this->createForm('root', 'POST', true);
319+
$form->add('items', CollectionType::class, [
320+
'entry_type' => ItemFileType::class,
321+
'allow_add' => true,
322+
]);
323+
324+
$file = $this->getUploadedFile();
325+
326+
$this->setRequestData('POST', [
327+
'root' => [
328+
'items' => [
329+
0 => [
330+
'item' => 'test',
331+
],
332+
],
333+
],
334+
], [
335+
'root' => [
336+
'items' => [
337+
0 => [
338+
'file' => $file,
339+
],
340+
],
341+
],
342+
]);
343+
344+
$this->requestHandler->handleRequest($form, $this->request);
345+
346+
$itemsForm = $form->get('items');
347+
348+
$this->assertTrue($form->isSubmitted());
349+
$this->assertTrue($form->isValid());
350+
351+
$this->assertTrue($itemsForm->has('0'));
352+
$this->assertFalse($itemsForm->has('1'));
353+
354+
$this->assertEquals('test', $itemsForm->get('0')->get('item')->getData());
355+
$this->assertNotNull($itemsForm->get('0')->get('file'));
356+
}
357+
314358
/**
315359
* @dataProvider methodExceptGetProvider
316360
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Type;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\Extension\Core\Type\FileType;
16+
use Symfony\Component\Form\Extension\Core\Type\TextType;
17+
use Symfony\Component\Form\FormBuilderInterface;
18+
19+
class ItemFileType extends AbstractType
20+
{
21+
public function buildForm(FormBuilderInterface $builder, array $options): void
22+
{
23+
$builder->add('item', TextType::class);
24+
$builder->add('file', FileType::class);
25+
}
26+
}

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

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

6155
foreach ($params as $key => $value) {
6256
if (\is_array($value) && \is_array($files[$key] ?? null)) {
@@ -65,6 +59,14 @@ public static function mergeParamsAndFiles(array $params, array $files): array
6559
}
6660
}
6761

68-
return array_replace($params, $files);
62+
if (!$isFilesList) {
63+
return array_replace($params, $files);
64+
}
65+
66+
foreach ($files as $value) {
67+
$params[] = $value;
68+
}
69+
70+
return $params;
6971
}
7072
}

0 commit comments

Comments
 (0)