Skip to content

Commit 4619258

Browse files
author
kells
committed
[Form] Keep submitted values when keep_as_list option of collection type is enabled
Co-authored-by: mariecharles marie.charles@hetic.net
1 parent 01bfc8d commit 4619258

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
@@ -207,7 +207,7 @@ public function onSubmit(FormEvent $event): void
207207
foreach ($formReindex as $index => $child) {
208208
$form->add($index, $this->type, array_replace([
209209
'property_path' => '['.$index.']',
210-
], $this->options));
210+
], $this->options, ['data' => $child->getData()]));
211211
}
212212
$data = array_values($data);
213213
}

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)