Skip to content

Commit

Permalink
[Form] Fix merging form data and files (ter)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Pintr authored and nicolas-grekas committed Oct 28, 2023
1 parent df8ff7f commit 8768ad1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
44 changes: 44 additions & 0 deletions Tests/AbstractRequestHandlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilder;
Expand All @@ -23,6 +24,7 @@
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\RequestHandlerInterface;
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\Form\Tests\Extension\Type\ItemFileType;
use Symfony\Component\Form\Util\ServerParams;

/**
Expand Down Expand Up @@ -311,6 +313,48 @@ public function testParamTakesPrecedenceOverFile($method)
$this->assertSame('DATA', $form->getData());
}

public function testMergeZeroIndexedCollection()
{
$form = $this->createForm('root', 'POST', true);
$form->add('items', CollectionType::class, [
'entry_type' => ItemFileType::class,
'allow_add' => true,
]);

$file = $this->getUploadedFile();

$this->setRequestData('POST', [
'root' => [
'items' => [
0 => [
'item' => 'test',
],
],
],
], [
'root' => [
'items' => [
0 => [
'file' => $file,
],
],
],
]);

$this->requestHandler->handleRequest($form, $this->request);

$itemsForm = $form->get('items');

$this->assertTrue($form->isSubmitted());
$this->assertTrue($form->isValid());

$this->assertTrue($itemsForm->has('0'));
$this->assertFalse($itemsForm->has('1'));

$this->assertEquals('test', $itemsForm->get('0')->get('item')->getData());
$this->assertNotNull($itemsForm->get('0')->get('file'));
}

/**
* @dataProvider methodExceptGetProvider
*/
Expand Down
26 changes: 26 additions & 0 deletions Tests/Extension/Type/ItemFileType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class ItemFileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('item', TextType::class);
$builder->add('file', FileType::class);
}
}
18 changes: 10 additions & 8 deletions Util/FormUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ public static function isEmpty($data)
*/
public static function mergeParamsAndFiles(array $params, array $files): array
{
if (array_is_list($files)) {
foreach ($files as $value) {
$params[] = $value;
}

return $params;
}
$isFilesList = array_is_list($files);

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

return array_replace($params, $files);
if (!$isFilesList) {
return array_replace($params, $files);
}

foreach ($files as $value) {
$params[] = $value;
}

return $params;
}
}

0 comments on commit 8768ad1

Please sign in to comment.