-
Notifications
You must be signed in to change notification settings - Fork 10
Description
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.