Type error caused by `FormBuilderIterator`
dmaicher opened this issue · comments
Environment
- PHP 8.1
- latest Symfony 6.1
- latest Sonata packages
Subject
Type error caused by FormBuilderIterator
Minimal repository with the bug
?
Steps to reproduce
I have an inline admin where I can add new items in a table view. If I press the "add" button then I get a type error since I moved to Symfony 6. It worked fine with Symfony 5.4 as there was no string
typehint on Symfony's FormBuilder::get
.
Actual results
A dump of
public function current(): FormBuilderInterface
{
if ($this->iterator->current() === 0) {
dd($this);
}
return $this->formBuilder->get($this->iterator->current());
}
From what I can tell we cannot guarantee that $this->iterator->current()
within FormBuilderIterator
will actually be a string. In my case its an integer and this seems legit. The problematic form field is an expanded choice field with checkboxes and the children of this form are indexed with integers.
I checked it and casting to string there solves the issue for me. Shall I create a PR for this?
public function current(): FormBuilderInterface
{
return $this->formBuilder->get((string) $this->iterator->current());
}
Might be related to symfony/symfony#46698 @VincentLanglet
Yeah, it was reported here sonata-project/form-extensions#368 (comment)