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

Skip to content

Commit 5ba3046

Browse files
bug #47505 [Mime] Form field values with integer keys not resolved correctly (claudiu-cristea)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Mime] Form field values with integer keys not resolved correctly | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #47504 | License | MIT | Doc PR | This should be legit: ``` new FormDataPart([ 'qux' => [ [ 'foo' => 'v1', 'baz' => 'v2', ], [ 'foo' => 'v3', 'baz' => 'v4', ], ], ]); ``` Fixes #47504 Commits ------- 923de03 [Mime] Form field values with integer keys not resolved correctly
2 parents 6470cc4 + 923de03 commit 5ba3046

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/Symfony/Component/Mime/Part/Multipart/FormDataPart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function prepareFields(array $fields): array
5858
$values = [];
5959

6060
$prepare = function ($item, $key, $root = null) use (&$values, &$prepare) {
61-
if (\is_int($key) && \is_array($item)) {
61+
if (null === $root && \is_int($key) && \is_array($item)) {
6262
if (1 !== \count($item)) {
6363
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)));
6464
}

src/Symfony/Component/Mime/Tests/Part/Multipart/FormDataPartTest.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testNestedArrayParts()
7979
],
8080
],
8181
['quux2' => clone $p1],
82-
['quux2' => clone $p1],
82+
['quux2' => [clone $p1]],
8383
'quuz2' => [
8484
['corge' => clone $p1],
8585
['corge' => clone $p1],
@@ -93,6 +93,17 @@ public function testNestedArrayParts()
9393
['2[1]' => clone $p1],
9494
['0[0]' => clone $p1],
9595
['0[1]' => clone $p1],
96+
97+
'qux' => [
98+
[
99+
'foo' => clone $p1,
100+
'bar' => clone $p1,
101+
],
102+
[
103+
'foo' => clone $p1,
104+
'bar' => clone $p1,
105+
],
106+
],
96107
]);
97108

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

129140
$parts[] = $p10 = clone $p1;
130-
$p10->setName('bar2[baz]');
141+
$p10->setName('bar2[0][baz]');
131142

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

135146
$parts[] = $p12 = clone $p1;
136147
$p12->setName('quux2');
137148
$parts[] = $p13 = clone $p1;
138-
$p13->setName('quux2');
149+
$p13->setName('quux2[0]');
139150

140151
$parts[] = $p14 = clone $p1;
141-
$p14->setName('quuz2[corge]');
152+
$p14->setName('quuz2[0][corge]');
142153
$parts[] = $p15 = clone $p1;
143-
$p15->setName('quuz2[corge]');
154+
$p15->setName('quuz2[1][corge]');
144155

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

176+
$parts[] = $p20 = clone $p1;
177+
$p20->setName('qux[0][foo]');
178+
$parts[] = $p21 = clone $p1;
179+
$p21->setName('qux[0][bar]');
180+
$parts[] = $p22 = clone $p1;
181+
$p22->setName('qux[1][foo]');
182+
$parts[] = $p23 = clone $p1;
183+
$p23->setName('qux[1][bar]');
184+
165185
$this->assertEquals($parts, $f->getParts());
166186
}
167187

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

178198
$this->expectException(InvalidArgumentException::class);
179199
$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.');
180-
181200
$f->getParts();
182201
}
183202

0 commit comments

Comments
 (0)