Skip to content

Commit 5c32ac1

Browse files
committed
Merge branch '6.3' into 6.4
* 6.3: [Form] Fix merging form data and files (ter) [Intl] Update the ICU data to 74.1 [Scheduler] Use MockClock [HtmlSanitizer] Consider `width` attribute as safe [DoctrineBridge] Fix exception message [Security][Validator] Missing translations for Luxembourgish
2 parents 5a1aa07 + e6743d1 commit 5c32ac1

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

Tests/AbstractRequestHandlerTestCase.php

+44
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
/**
@@ -302,6 +304,48 @@ public function testParamTakesPrecedenceOverFile($method)
302304
$this->assertSame('DATA', $form->getData());
303305
}
304306

307+
public function testMergeZeroIndexedCollection()
308+
{
309+
$form = $this->createForm('root', 'POST', true);
310+
$form->add('items', CollectionType::class, [
311+
'entry_type' => ItemFileType::class,
312+
'allow_add' => true,
313+
]);
314+
315+
$file = $this->getUploadedFile();
316+
317+
$this->setRequestData('POST', [
318+
'root' => [
319+
'items' => [
320+
0 => [
321+
'item' => 'test',
322+
],
323+
],
324+
],
325+
], [
326+
'root' => [
327+
'items' => [
328+
0 => [
329+
'file' => $file,
330+
],
331+
],
332+
],
333+
]);
334+
335+
$this->requestHandler->handleRequest($form, $this->request);
336+
337+
$itemsForm = $form->get('items');
338+
339+
$this->assertTrue($form->isSubmitted());
340+
$this->assertTrue($form->isValid());
341+
342+
$this->assertTrue($itemsForm->has('0'));
343+
$this->assertFalse($itemsForm->has('1'));
344+
345+
$this->assertEquals('test', $itemsForm->get('0')->get('item')->getData());
346+
$this->assertNotNull($itemsForm->get('0')->get('file'));
347+
}
348+
305349
/**
306350
* @dataProvider methodExceptGetProvider
307351
*/

Tests/Extension/Type/ItemFileType.php

+26
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+
}

Util/FormUtil.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,7 @@ public static function isEmpty(mixed $data): bool
4646
*/
4747
public static function mergeParamsAndFiles(array $params, array $files): array
4848
{
49-
if (array_is_list($files)) {
50-
foreach ($files as $value) {
51-
$params[] = $value;
52-
}
53-
54-
return $params;
55-
}
49+
$isFilesList = array_is_list($files);
5650

5751
foreach ($params as $key => $value) {
5852
if (\is_array($value) && \is_array($files[$key] ?? null)) {
@@ -61,6 +55,14 @@ public static function mergeParamsAndFiles(array $params, array $files): array
6155
}
6256
}
6357

64-
return array_replace($params, $files);
58+
if (!$isFilesList) {
59+
return array_replace($params, $files);
60+
}
61+
62+
foreach ($files as $value) {
63+
$params[] = $value;
64+
}
65+
66+
return $params;
6567
}
6668
}

0 commit comments

Comments
 (0)