How do you hide labels in a form class in symfony2? How do you hide labels in a form class in symfony2? symfony symfony

How do you hide labels in a form class in symfony2?


Since Symfony 2.2 you can avoid the <label> rendering using the false value for the label attribute:

public function buildForm(FormBuilderInterface $builder, array $options){    $builder        ->add('Name', null, array('label' => false))    ;}

Source


Keep your 'View' specifications separate from your 'Model'

If you follow the accepted answer which says:

$builder        ->add('Name', null, array('label' => false))    ;

your form is not as re-usable. Especially if your form appears in more than one location (or might in the future).

If you do not want to render the form label it is best to do so in Twig (assuming your using Twig).

instead of rendering {{ form_row(form.name) }}, render each element separetly and exclude the form_label

ex.

{{ form_errors(form.name) }} {# {{ form_label(form.name) }} <-- just dont include this #} {{ form_widget(form.name) }}

If down the road you wanted the label in one instance of the form but the not the other, simply adding {{ form_label(form.name) }} would suffice; Where as changing array('label' => true) would turn the label on everywhere

If you are rendering your form with the one liner {{ form(form) }} then you should have a look at the symfony docs


Just add {'label':false} to your form_row()

{{ form_row(form.name, {'label':false}) }}