Skip to content

Form field values with integer keys not resolved correctly #47505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2023
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mime/Part/Multipart/FormDataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function prepareFields(array $fields): array
$values = [];

$prepare = function ($item, $key, $root = null) use (&$values, &$prepare) {
if (\is_int($key) && \is_array($item)) {
if (null === $root && \is_int($key) && \is_array($item)) {
if (1 !== \count($item)) {
throw new InvalidArgumentException(sprintf('Form field values with integer keys can only have one array element, the key being the field name and the value being the field value, %d provided.', \count($item)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function testNestedArrayParts()
],
],
['quux2' => clone $p1],
['quux2' => clone $p1],
['quux2' => [clone $p1]],
'quuz2' => [
['corge' => clone $p1],
['corge' => clone $p1],
Expand All @@ -93,6 +93,17 @@ public function testNestedArrayParts()
['2[1]' => clone $p1],
['0[0]' => clone $p1],
['0[1]' => clone $p1],

'qux' => [
[
'foo' => clone $p1,
'bar' => clone $p1,
],
[
'foo' => clone $p1,
'bar' => clone $p1,
],
],
]);

$this->assertEquals('multipart', $f->getMediaType());
Expand Down Expand Up @@ -127,20 +138,20 @@ public function testNestedArrayParts()
$p9->setName('0');

$parts[] = $p10 = clone $p1;
$p10->setName('bar2[baz]');
$p10->setName('bar2[0][baz]');

$parts[] = $p11 = clone $p1;
$p11->setName('bar2[baz][qux]');

$parts[] = $p12 = clone $p1;
$p12->setName('quux2');
$parts[] = $p13 = clone $p1;
$p13->setName('quux2');
$p13->setName('quux2[0]');

$parts[] = $p14 = clone $p1;
$p14->setName('quuz2[corge]');
$p14->setName('quuz2[0][corge]');
$parts[] = $p15 = clone $p1;
$p15->setName('quuz2[corge]');
$p15->setName('quuz2[1][corge]');

$parts[] = $p16 = clone $p1;
$p16->setName('2');
Expand All @@ -162,6 +173,15 @@ public function testNestedArrayParts()
$parts[] = $p19 = clone $p1;
$p19->setName('0[1]');

$parts[] = $p20 = clone $p1;
$p20->setName('qux[0][foo]');
$parts[] = $p21 = clone $p1;
$p21->setName('qux[0][bar]');
$parts[] = $p22 = clone $p1;
$p22->setName('qux[1][foo]');
$parts[] = $p23 = clone $p1;
$p23->setName('qux[1][bar]');

$this->assertEquals($parts, $f->getParts());
}

Expand All @@ -177,7 +197,6 @@ public function testExceptionOnFormFieldsWithIntegerKeysAndMultipleValues()

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Form field values with integer keys can only have one array element, the key being the field name and the value being the field value, 2 provided.');

$f->getParts();
}

Expand Down