8000 [Form] Fix `keep_as_list` when data is not an array by MatTheCat · Pull Request #60638 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix keep_as_list when data is not an array #60638

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
Jun 10, 2025
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
8000
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ public function onSubmit(FormEvent $event): void
}

if ($this->keepAsList) {
$formReindex = [];
$formReindex = $dataKeys = [];
foreach ($data as $key => $value) {
$dataKeys[] = $key;
}
foreach ($dataKeys as $key) {
unset($data[$key]);
}
foreach ($form as $name => $child) {
$formReindex[] = $child;
$form->remove($name);
Expand All @@ -208,8 +214,8 @@ public function onSubmit(FormEvent $event): void
$form->add($index, $this->type, array_replace([
'property_path' => '['.$index.']',
], $this->options));
$data[$index] = $child->getData();
}
$data = array_values($data);
}

$event->setData($data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public function testOnSubmitDealsWithObjectBackedIteratorAggregate()
$this->assertArrayNotHasKey(2, $event->getData());
}

public function testOnSubmitDealsWithArrayBackedIteratorAggregate()
public function testOnSubmitDealsWithDoctrineCollection()
{
$this->builder->add($this->getBuilder('1'));

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

public function testKeepAsListWorksWithTraversableArrayAccess()
{
$this->builder->add($this->getBuilder('1' 6F16 ));

$data = new \ArrayIterator([0 => 'first', 1 => 'second', 2 => 'third']);
$event = new FormEvent($this->builder->getForm(), $data);
$listener = new ResizeFormListener(TextType::class, keepAsList: true);
$listener->onSubmit($event);

$this->assertCount(1, $event->getData());
$this->assertArrayHasKey(0, $event->getData());
}

public function testOnSubmitDeleteEmptyNotCompoundEntriesIfAllowDelete()
{
$this->builder->setData(['0' => 'first', '1' => 'second']);
Expand Down
Loading
0