Skip to content

Commit

Permalink
Merge branch '5.4' into 6.3
Browse files Browse the repository at this point in the history
* 5.4:
  [Form] Fix merging params & files when "multiple" is enabled
  [HttpFoundation] Do not swallow trailing `=` in cookie value
  Handle Sendinblue error responses without a message key
  [Serializer] Fix collecting only first missing constructor argument
  [Messenger] Fix DoctrineOpenTransactionLoggerMiddleware
  [Translation] Add missing return type
  [Validator] add missing catalan translations
  • Loading branch information
nicolas-grekas committed Oct 17, 2023
2 parents 1fa2ae7 + 54c8fd4 commit 6af3c25
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Extension/HttpFoundation/HttpFoundationRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\RequestHandlerInterface;
use Symfony\Component\Form\Util\FormUtil;
use Symfony\Component\Form\Util\ServerParams;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function handleRequest(FormInterface $form, mixed $request = null)
}

if (\is_array($params) && \is_array($files)) {
$data = array_replace_recursive($params, $files);
$data = FormUtil::mergeParamsAndFiles($params, $files);
} else {
$data = $params ?: $files;
}
Expand Down
3 changes: 2 additions & 1 deletion NativeRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form;

use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Util\FormUtil;
use Symfony\Component\Form\Util\ServerParams;

/**
Expand Down Expand Up @@ -106,7 +107,7 @@ public function handleRequest(FormInterface $form, mixed $request = null)
}

if (\is_array($params) && \is_array($files)) {
$data = array_replace_recursive($params, $files);
$data = FormUtil::mergeParamsAndFiles($params, $files);
} else {
$data = $params ?: $files;
}
Expand Down
36 changes: 36 additions & 0 deletions Tests/AbstractRequestHandlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,42 @@ public function testMergeParamsAndFiles($method)
$this->assertSame($file, $form->get('field2')->getData());
}

/**
* @dataProvider methodExceptGetProvider
*/
public function testMergeParamsAndFilesMultiple($method)
{
$form = $this->createForm('param1', $method, true);
$form->add($this->createBuilder('field1', false, ['allow_file_upload' => true, 'multiple' => true])->getForm());
$file1 = $this->getUploadedFile();
$file2 = $this->getUploadedFile();

$this->setRequestData($method, [
'param1' => [
'field1' => [
'foo',
'bar',
'baz',
],
],
], [
'param1' => [
'field1' => [
$file1,
$file2,
],
],
]);

$this->requestHandler->handleRequest($form, $this->request);
$data = $form->get('field1')->getData();

$this->assertTrue($form->isSubmitted());
$this->assertIsArray($data);
$this->assertCount(5, $data);
$this->assertSame(['foo', 'bar', 'baz', $file1, $file2], $data);
}

/**
* @dataProvider methodExceptGetProvider
*/
Expand Down
25 changes: 25 additions & 0 deletions Util/FormUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,29 @@ public static function isEmpty(mixed $data): bool
// not considered to be empty, ever.
return null === $data || '' === $data;
}

/**
* Recursively replaces or appends elements of the first array with elements
* of second array. If the key is an integer, the values will be appended to
* the new array; otherwise, the value from the second array will replace
* the one from the first array.
*/
public static function mergeParamsAndFiles(array $params, array $files): array
{
$result = [];

foreach ($params as $key => $value) {
if (\is_array($value) && \is_array($files[$key] ?? null)) {
$value = self::mergeParamsAndFiles($value, $files[$key]);
unset($files[$key]);
}
if (\is_int($key)) {
$result[] = $value;
} else {
$result[$key] = $value;
}
}

return array_merge($result, $files);
}
}

0 comments on commit 6af3c25

Please sign in to comment.