Sam
Sam

Reputation: 1828

symfony form CheckboxType inside CollectionType = prototype empty

I am trying to use a CheckBoxType in a form inside a CollectionType but the prototype only contains the label and no checkbox at all !

This is really weird; I don't understand because the symfony documentation here does not mention anything special to do ?

When I click on the button to add a new file I only get the label

What am I missing please?

THE COLLECTION

->add('file', CollectionType::class, array(
    'label' => false,
    'entry_type' => FileType::class,
    'error_bubbling' => false,
    'entry_options' => [ 'required' => false, 'error_bubbling' => true, ],
    'allow_add' => true,
    'allow_delete' => true
))

THE CHECKBOX INSIDE FILETYPE

->add('main', CheckboxType::class,[
     'label'    => 'Make this one the main picture',
     'required' => false,
]);

THE RESULTING PROTOTYPE

data-prototype="<div id=\"new_item_group_pictures_itemFile___name__\">    <div class=\"form-item\"><div class=\"form-label\"></div><div class=\"custom-select\"><input type=\"file\" id=\"new_item_group_pictures_itemFile___name___file\" name=\"new_item[group_pictures][itemFile][__name__][file]\" class=\"form-file\" /></div></div>    <div class=\"form-item\"><div class=\"form-label\"><label for=\"new_item_group_pictures_itemFile___name___description\">Description</label></div><input type=\"text\" id=\"new_item_group_pictures_itemFile___name___description\" name=\"new_item[group_pictures][itemFile][__name__][description]\" maxlength=\"255\" class=\"form-input form-text\" /></div>    <div class=\"form-item\"><div class=\"form-label\"><label for=\"new_item_group_pictures_itemFile___name___main\">Make this one the main picture</label></div></div></div>"

ENTITY PROPERTY

 /**
 * @var boolean
 * @ORM\Column(type="boolean", nullable=true)
 */
private $main;

Upvotes: 0

Views: 1002

Answers (2)

hoover_D
hoover_D

Reputation: 650

you have to render your embeded form collection iterating over it:

{% for t in form.file %}
    {{ form_row(t) }}
{% endfor %}

This will render each checkbox and you can manipulate them in your view

Normally checkboxType should work fine, but if you want to try another solution you can set your second form to ChoiceType and add expanded (to expand choices like checkboxes) and multiple true.

->add('main',  ChoiceType::class,[
     'label'    => 'Make this one the main picture',
     'required' => false,
     'choices' => array(),
     'expanded' => true,
     'multiple' => true
]);

Upvotes: 1

Alexander Dimitrov
Alexander Dimitrov

Reputation: 974

The label of the collection type should be true and when you render the form field of the collection type use form_widget instead of form_row

Upvotes: 0

Related Questions