8000 bug #59910 [Form] Keep submitted values when `keep_as_list` option of… · symfony/symfony@ac794a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac794a7

Browse files
committed
bug #59910 [Form] Keep submitted values when keep_as_list option of collection type is enabled (kells)
This PR was merged into the 7.2 branch. Discussion ---------- [Form] Keep submitted values when `keep_as_list` option of collection type is enabled | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #57430 | License | MIT When the `keep_as_list` option of `CollectionType` is true, its entries lose their values when re-displaying the form with submitted data. This fix only concerns the 2nd point of the [linked issue](#57430 (comment)). Commits ------- bac56de [Form] Keep submitted values when keep_as_list option of collection type is enabled
2 parents 455a7e2 + bac56de commit ac794a7

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function onSubmit(FormEvent $event): void
213213
foreach ($formReindex as $index => $child) {
214214
$form->add($index, $this->type, array_replace([
215215
'property_path' => '['.$index.']',
216-
], $this->options));
216+
], $this->options, ['data' => $child->getData()]));
217217
$data[$index] = $child->getData();
218218
}
219219
}

src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public function testCollectionTypeKeepAsListOptionTrue()
257257
{
258258
$formMetadata = new ClassMetadata(Form::class);
259259
$authorMetadata = (new ClassMetadata(Author::class))
260-
->addPropertyConstraint('firstName', new NotBlank());
260+
->addPropertyConstraint('firstName', new Length(1));
261261
$organizationMetadata = (new ClassMetadata(Organization::class))
262262
->addPropertyConstraint('authors', new Valid());
263263
$metadataFactory = $this->createMock(MetadataFactoryInterface::class);
@@ -301,22 +301,22 @@ public function testCollectionTypeKeepAsListOptionTrue()
301301
$form->submit([
302302
'authors' => [
303303
0 => [
304-
'firstName' => '', // Fires a Not Blank Error
304+
'firstName' => 'foobar', // Fires a Length Error
305305
'lastName' => 'lastName1',
306306
],
307307
// key "1" could be missing if we add 4 blank form entries and then remove it.
308308
2 => [
309-
'firstName' => '', // Fires a Not Blank Error
309+
'firstName' => 'barfoo', // Fires a Length Error
310310
'lastName' => 'lastName3',
311311
],
312312
3 => [
313-
'firstName' => '', // Fires a Not Blank Error
313+
'firstName' => 'barbaz', // Fires a Length Error
314314
'lastName' => 'lastName3',
315315
],
316316
],
317317
]);
318318

319-
// Form does have 3 not blank errors
319+
// Form does have 3 length errors
320320
$errors = $form->getErrors(true);
321321
$this->assertCount(3, $errors);
322322

@@ -328,12 +328,15 @@ public function testCollectionTypeKeepAsListOptionTrue()
328328
];
329329

330330
$this->assertTrue($form->get('authors')->has('0'));
331+
$this->assertSame('foobar', $form->get('authors')->get('0')->getData()->firstName);
331332
$this->assertContains('data.authors[0].firstName', $errorPaths);
332333

333334
$this->assertTrue($form->get('authors')->has('1'));
335+
$this->assertSame('barfoo', $form->get('authors')->get('1')->getData()->firstName);
334336
$this->assertContains('data.authors[1].firstName', $errorPaths);
335337

336338
$this->assertTrue($form->get('authors')->has('2'));
339+
$this->assertSame('barbaz', $form->get('authors')->get('2')->getData()->firstName);
337340
$this->assertContains('data.authors[2].firstName', $errorPaths);
338341

339342
$this->assertFalse($form->get('authors')->has('3'));

0 commit comments

Comments
 (0)
0