sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Type error caused by `FormBuilderIterator` #7984

Closed dmaicher closed 1 year ago

dmaicher commented 1 year ago

Environment

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());
    }
dmaicher commented 1 year ago

Might be related to https://github.com/symfony/symfony/issues/46698 @VincentLanglet :thinking:

VincentLanglet commented 1 year ago

Yeah, it was reported here https://github.com/sonata-project/form-extensions/issues/368#issuecomment-1337562206