I have a form in which I want to have checkboxes to add or remove elements from a collection: a User which have Responsability[].
I want to show some of the existing Responsability in the form but not all of them. I use an attribute called automatic to determine if I want to display them or not.
How can I edit my form to do such a thing?
UserType.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', TextType::class, [
'label' => 'Nom d\'utilisateurice'
])
->add('responsibilities', EntityType::class, [
// looks for choices from this entity
'class' => Responsibility::class,
// uses the Responsibility.label property as the visible option string
'choice_label' => 'label',
'label' => 'Rôles',
'multiple' => true,
'expanded' => true,
'choice_attr' => function($responsibility)
{
return [
'data-responsibility-description' => $responsibility->getDescription(),
];
},
])
->add('submit',SubmitType::class, [
'label' => 'Changer les informations',
'attr' => [
'class' => 'btn btn-outline-primary float-right'
]
]);
}
edit.html.twig:
{{ form_start(edit_form, {'attr': {'id': 'form-edit-user'}}) }}
<div class="form-group">
{{ form_label(edit_form.username) }}
{{ form_widget(edit_form.username) }}
</div>
<div class="form-group">
{{ form_label(edit_form.responsibilities) }}
{% for responsibility in edit_form.responsibilities %}
<div class="form-group">
{{ form_widget(responsibility) }}
{{ form_label(responsibility) }}
<span class="text-muted responsibility-description">
{{ responsibility.vars.attr['data-responsibility-description'] }}
</span>
</div>
{% endfor %}
</div>
{{ form_widget(edit_form) }}
{{ form_end(edit_form) }}
automaticpart of the Responsibility-entity and what are the possible values, I guess true/false?autotomaticis a Responsability attribute and has getter and setter. It's possible values are true or false.