Skip to content

Data option set to null when dependant removed #10

@robtromans

Description

@robtromans

Apologies if I am missing something obvious.

I have a selectbox which determines if my LiveCollectionType shows. I need the data option to either use the saved data or be an empty array (so that at least one TextType from the collection always shows, just like the embedded CollectionType Form example in the symfony live components demo).

I am finding that everytime the LiveCollectionType gets hidden by the selectbox's value (elementType) the data option gets set to null and so I have nothing to show the user.

   ->addDependent('choices', 'elementType', function (DependentField $field, ?FormCreatorElementType $elementType) use ($element) {
                if (FormCreatorElementType::MultipleChoice !== $elementType) {
                    return;
                }

                $field
                    ->add(LiveCollectionType::class, [
                        'entry_type' => ElementChoice::class,
                        'label' => false,
                        'constraints' => [
                            new NotBlank([
                                'message' => 'You must enter a choice',
                            ]),
                        ],

                        // This option gets set to null when this field is not returned
                        'data' => $element->getChoices() ?? [[]],
                    ])
                ;
            })

If I remove the multiplechoice !== $elementType check, data gets set as an empty array and therefore shows at least one ElementChoice object in my form but as soon as its removed and shown again based on the dependancy it seems to get set to null, thus removing the empty collection textType.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions