Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form] Fix merging params & files when "multiple" is enabled
If a form field is configured with the "multiple" option, the request
handler merges param values with uploaded files. However, it does not
merge correctly if "multiple" is enabled.

Example:

    <input type="file" name="param[]" />
    <input type="hidden" name="param[]" value="data1" />
    <input type="hidden" name="param[]" value="data2" />

With the example, the file field will incorrectly replace the first
hidden field, when they should be merged. This commit fixes the problem.
  • Loading branch information
priyadi authored and nicolas-grekas committed Oct 17, 2023
commit 4da09bc891d0c11a8724f1c5ffc559525272634a
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, $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 src/Symfony/Component/Form/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, $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
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 src/Symfony/Component/Form/Util/FormUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,29 @@ public static function isEmpty($data)
// 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);
}
}