Skip to content

Commit

Permalink
Merge branch '6.4' into 7.0
Browse files Browse the repository at this point in the history
* 6.4:
  [Form] Fix merging form data and files (ter)
  [Intl] Update the ICU data to 74.1
  [Scheduler] Use MockClock
  [AssetMapper] Fixing memory bug where we stored way more file content than needed
  [AssetMapper] jsdelivr "no version" import syntax
  [HtmlSanitizer] Consider `width` attribute as safe
  [DoctrineBridge] Fix exception message
  [Security][Validator] Missing translations for Luxembourgish
  • Loading branch information
fabpot committed Oct 28, 2023
2 parents ebaccca + 5c32ac1 commit 65c5ac5
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 @@ -302,6 +304,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 <fabien@symfony.com>
*
* 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 @@ -46,13 +46,7 @@ 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;
}

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

foreach ($params as $key => $value) {
if (\is_array($value) && \is_array($files[$key] ?? null)) {
Expand All @@ -61,6 +55,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 65c5ac5

Please # to comment.