Skip to content

Commit e1ccd76

Browse files
Merge branch '6.3' into 6.4
* 6.3: [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] Simplify code [Messenger] Fix graceful exit [Messenger] Fix DoctrineOpenTransactionLoggerMiddleware [Translation] Add missing return type [Validator] add missing catalan translations
2 parents caebc88 + 6af3c25 commit e1ccd76

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

Extension/HttpFoundation/HttpFoundationRequestHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\FormError;
1616
use Symfony\Component\Form\FormInterface;
1717
use Symfony\Component\Form\RequestHandlerInterface;
18+
use Symfony\Component\Form\Util\FormUtil;
1819
use Symfony\Component\Form\Util\ServerParams;
1920
use Symfony\Component\HttpFoundation\File\File;
2021
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -95,7 +96,7 @@ public function handleRequest(FormInterface $form, mixed $request = null)
9596
}
9697

9798
if (\is_array($params) && \is_array($files)) {
98-
$data = array_replace_recursive($params, $files);
99+
$data = FormUtil::mergeParamsAndFiles($params, $files);
99100
} else {
100101
$data = $params ?: $files;
101102
}

NativeRequestHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form;
1313

1414
use Symfony\Component\Form\Exception\UnexpectedTypeException;
15+
use Symfony\Component\Form\Util\FormUtil;
1516
use Symfony\Component\Form\Util\ServerParams;
1617

1718
/**
@@ -106,7 +107,7 @@ public function handleRequest(FormInterface $form, mixed $request = null)
106107
}
107108

108109
if (\is_array($params) && \is_array($files)) {
109-
$data = array_replace_recursive($params, $files);
110+
$data = FormUtil::mergeParamsAndFiles($params, $files);
110111
} else {
111112
$data = $params ?: $files;
112113
}

Tests/AbstractRequestHandlerTestCase.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,42 @@ public function testMergeParamsAndFiles($method)
227227
$this->assertSame($file, $form->get('field2')->getData());
228228
}
229229

230+
/**
231+
* @dataProvider methodExceptGetProvider
232+
*/
233+
public function testMergeParamsAndFilesMultiple($method)
234+
{
235+
$form = $this->createForm('param1', $method, true);
236+
$form->add($this->createBuilder('field1', false, ['allow_file_upload' => true, 'multiple' => true])->getForm());
237+
$file1 = $this->getUploadedFile();
238+
$file2 = $this->getUploadedFile();
239+
240+
$this->setRequestData($method, [
241+
'param1' => [
242+
'field1' => [
243+
'foo',
244+
'bar',
245+
'baz',
246+
],
247+
],
248+
], [
249+
'param1' => [
250+
'field1' => [
251+
$file1,
252+
$file2,
253+
],
254+
],
255+
]);
256+
257+
$this->requestHandler->handleRequest($form, $this->request);
258+
$data = $form->get('field1')->getData();
259+
260+
$this->assertTrue($form->isSubmitted());
261+
$this->assertIsArray($data);
262+
$this->assertCount(5, $data);
263+
$this->assertSame(['foo', 'bar', 'baz', $file1, $file2], $data);
264+
}
265+
230266
/**
231267
* @dataProvider methodExceptGetProvider
232268
*/

Util/FormUtil.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,29 @@ public static function isEmpty(mixed $data): bool
3737
// not considered to be empty, ever.
3838
return null === $data || '' === $data;
3939
}
40+
41+
/**
42+
* Recursively replaces or appends elements of the first array with elements
43+
* of second array. If the key is an integer, the values will be appended to
44+
* the new array; otherwise, the value from the second array will replace
45+
* the one from the first array.
46+
*/
47+
public static function mergeParamsAndFiles(array $params, array $files): array
48+
{
49+
$result = [];
50+
51+
foreach ($params as $key => $value) {
52+
if (\is_array($value) && \is_array($files[$key] ?? null)) {
53+
$value = self::mergeParamsAndFiles($value, $files[$key]);
54+
unset($files[$key]);
55+
}
56+
if (\is_int($key)) {
57+
$result[] = $value;
58+
} else {
59+
$result[$key] = $value;
60+
}
61+
}
62+
63+
return array_merge($result, $files);
64+
}
4065
}

0 commit comments

Comments
 (0)