I have a formType that contains an EntityType :: class. This is a list of possible types of users to choose from in a form.
->add('typeUser',EntityType::class, array(
'required' => true,
'class'=>'Site\PagesBundle\Entity\TypeUser',
'choice_label'=>'typeUtilisateur',
'expanded'=>true,
'multiple'=>true,
'empty_data' => 'Veuillez sélectionner au moins 1 type',
));
I wish I could give a "style" to these choices. For example, I would like them to be some distance from the checkbox.
So, I have to add style (margin-left by example) to :
'choice_label'=>'typeUtilisateur',
Is it possible in FormType ?
Or I should make that on my twig file ? In this case, how can I do it ?
Thanks for your help !
EDIT:
->add('typeUser',EntityType::class, array(
'required' => true,
'class'=>'Site\PagesBundle\Entity\TypeUser',
'choice_label'=>('typeUtilisateur' => 'attr' => array( 'style' => 'margin-left:15px')) ,
'expanded'=>true,
'multiple'=>true,
'empty_data' => 'Veuillez sélectionner au moins 1 type',
));
There are a few ways you can style your form fields. See a few of them below:
Option 1: Add a class attribute to the field in the form builder
$builder
->add('example', EntityType::class, [
'class' => FooBar::class,
'attr' => [
'class' => 'my-example-class'
]
])
Option 2: Specify a class in the form template
{{ form_widget(form.fooBar, {'attr': {'class': 'form-control'}}) }}
You can be specific to the choices, see this link here which shows adding a class attribute to choices.
I think that the right way is to use the attr key with which you could set a custom class="" for your field
->add('typeUser',EntityType::class, array(
'attr' => [
'class' => 'YOUR_CHOICE_CLASS',
],
'required' => true,
'class'=>'Site\PagesBundle\Entity\TypeUser',
'expanded'=>true,
'multiple'=>true,
'empty_data' => 'Veuillez sélectionner au moins 1 type',
));
Then, can define the styles that you want in your CSS file