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

PhpFormsClassSymfony

Php Problem Overview


I know that you can split a form out in twig and choose to not render the label for a particular field, but I can't help but think that you must be able to do this from the form class. The 'label' key in the options array lets you change this value to whatever you like, but passing either false or an empty string just returns the field name (see examples below where 'roles' is rendered as the label).

$builder
            ->add('roles', 'entity', array(
                'class' => 'Acme\UserBundle\Entity\Role',
                'label' => '' 
            ));

$builder
            ->add('roles', 'entity', array(
                'class' => 'Acme\UserBundle\Entity\Role',
                'label' => false 
            ));

Strangely, passing an empty space (which feels very dirty) seems to render a completely empty label, with no space even when viewing the source. Can anyone shed any light on the best approach, or even why the empty space seems to work?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

> 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

Solution 3 - Php

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

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

Solution 4 - Php

I don't understand very well your question but in form to show the name of label,personnaly I do like that :

  $builder
        ->add('role', 'text')

in my twig :

    <tr>
        <td>{{ form_widget(form.role) }} </td>
        <td>{{ form_label(form.role, "Name of Label") }}</td>
    </tr>
    <tr>
        <td>{{ form_errors(form.role) }}</td>
    </tr>

Solution 5 - Php

To hide my label, I had to render just the widget for the field, and not the label, e.g.

{{ form_widget(edit_form.event) }}
{{ form_rest(edit_form) }}

The problem with the ' ' label with a space in, is that it still renders the html input which is there and affects the page.

Solution 6 - Php

this should work (although its not a very clean solution)

$builder
        ->add('roles', 'entity', array(
            'class' => 'Acme\UserBundle\Entity\Role',
            'label' => ' ' 
        ));

(note the space between the ticks)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkView Question on Stackoverflow
Solution 1 - PhpcheesemacflyView Answer on Stackoverflow
Solution 2 - PhpChrisView Answer on Stackoverflow
Solution 3 - PhpPrestonDocksView Answer on Stackoverflow
Solution 4 - PhpNllView Answer on Stackoverflow
Solution 5 - PhpjmozView Answer on Stackoverflow
Solution 6 - PhpphilipphoffmannView Answer on Stackoverflow