Skip to content

Commit e06b02d

Browse files
Merge branch '7.2' into 7.3
* 7.2: Silence E_DEPRECATED and E_USER_DEPRECATED [HttpCache] Hit the backend only once after waiting for the cache lock fix compatibility with Symfony 7.4 [Form] Keep submitted values when keep_as_list option of collection type is enabled [Form] Fix `keep_as_list` when data is not an array
2 parents d472aa0 + 4798286 commit e06b02d

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

Extension/Core/EventListener/ResizeFormListener.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,23 @@ public function onSubmit(FormEvent $event): void
199199
}
200200

201201
if ($this->keepAsList) {
202-
$formReindex = [];
202+
$formReindex = $dataKeys = [];
203+
foreach ($data as $key => $value) {
204+
$dataKeys[] = $key;
205+
}
206+
foreach ($dataKeys as $key) {
207+
unset($data[$key]);
208+
}
203209
foreach ($form as $name => $child) {
204210
$formReindex[] = $child;
205211
$form->remove($name);
206212
}
207213
foreach ($formReindex as $index => $child) {
208214
$form->add($index, $this->type, array_replace([
209215
'property_path' => '['.$index.']',
210-
], $this->options));
216+
], $this->options, ['data' => $child->getData()]));
217+
$data[$index] = $child->getData();
211218
}
212-
$data = array_values($data);
213219
}
214220

215221
$event->setData($data);

Tests/Extension/Core/EventListener/ResizeFormListenerTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public function testOnSubmitDealsWithObjectBackedIteratorAggregate()
310310
$this->assertArrayNotHasKey(2, $event->getData());
311311
}
312312

313-
public function testOnSubmitDealsWithArrayBackedIteratorAggregate()
313+
public function testOnSubmitDealsWithDoctrineCollection()
314314
{
315315
$this->builder->add($this->getBuilder('1'));
316316

@@ -323,6 +323,19 @@ public function testOnSubmitDealsWithArrayBackedIteratorAggregate()
323323
$this->assertArrayNotHasKey(2, $event->getData());
324324
}
325325

326+
public function testKeepAsListWorksWithTraversableArrayAccess()
327+
{
328+
$this->builder->add($this->getBuilder('1'));
329+
330+
$data = new \ArrayIterator([0 => 'first', 1 => 'second', 2 => 'third']);
331+
$event = new FormEvent($this->builder->getForm(), $data);
332+
$listener = new ResizeFormListener(TextType::class, keepAsList: true);
333+
$listener->onSubmit($event);
334+
335+
$this->assertCount(1, $event->getData());
336+
$this->assertArrayHasKey(0, $event->getData());
337+
}
338+
326339
public function testOnSubmitDeleteEmptyNotCompoundEntriesIfAllowDelete()
327340
{
328341
$this->builder->setData(['0' => 'first', '1' => 'second']);

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)