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 eedb07a

Browse filesBrowse files
Jan Pintrnicolas-grekas
Jan Pintr
authored andcommitted
[Form] Fix merging form data and files (ter)
1 parent 4a41159 commit eedb07a
Copy full SHA for eedb07a

File tree

3 files changed

+80
-8
lines changed
Filter options

3 files changed

+80
-8
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/AbstractRequestHandlerTestCase.php
+44Lines 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
*/
+26Lines 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 <fabien@symfony.com>
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Util/FormUtil.php
+10-8Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.