sonata-project / SonataAdminBundle

The missing Symfony Admin Generator

Home Page:https://docs.sonata-project.org/projects/SonataAdminBundle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Screenshot from 2022-12-08 13-09-45

A dump of

    public function current(): FormBuilderInterface
    {
        if ($this->iterator->current() === 0) {
            dd($this);
        }

        return $this->formBuilder->get($this->iterator->current());
    }

Screenshot from 2022-12-08 13-10-26

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());
    }