Mikhail
Mikhail

Reputation: 2562

Symfony2 how to remove label for the field using FormBuilder

$builder->add('body','text',array('label' => FALSE)//default label is displayed
$builder->add('body','text',array('label' => '')//default label is displayed
$builder->add('body','text',array('label' => 0)//default label is displayed
$builder->add('body','text',array('label' => ' ')//empty label is displayed

But I don't need to render a label tag. I use a form_widget(form) inthe view and I can't use a form_row(form.field1) ... form_row(form.field25) to display a forms. I want to remove label only using a FormBuilder. It's possible?

Upvotes: 2

Views: 10984

Answers (4)

zoore
zoore

Reputation: 330

better solution is just:

[..Type.php]

$builder
            ->add('email', EmailType::class ) //will display default label
            ->add('username', TextType::class,
                array(
                    'label' => false,
                    'attr' => array(
//                      'class' => 'myclassfrom.css', //<- this one is realy avesome
                        'placeholder' => 'UsernameExample',
                        'autofocus' => '',

                    ),
            ))

and all You need is just to put in your related twig

 {{ form_start(form) }}
                        {{ form_row(form.username) }}
 {{ form_end(form) }}

Upvotes: 2

Oleksii Zymovets
Oleksii Zymovets

Reputation: 740

I have a collection of collection fields in my form (collection inside of a collection). And I had a problem with entry labels with collection fields. For every entry of collection the labels were automatically generated as [0], [1], ... [N]. The only thing I had to do to get rid of it was to add blank label blocks to the template:

{% form_theme form _self %}

{% block _myformname_person_entry_label %}
{% endblock _myformname_person_entry_label %}

{% block _myformname_person_entry_adress_entry_label %}
{% endblock _myformname_person_entry_adress_entry_label %}

This didn't override any of labels, specified in {% block _myformname_person_row %}, {% block _myformname_person_widget %} or {% block _myformname_person %} blocks in template or in the builder class:

$builder->add('person', 'collection', array(
'type' => new PersonType(),
'label' => 'List of Employees:')

Tested on Symfony version 2.3

Upvotes: 0

Olivier
Olivier

Reputation: 41

I just tested this working solution with sf2.4.6 :

$builder->add('body','text',array('label' => false);

This solution is better than

label => ' '

which is just rendering a single space. Moreover you don't need to split your rendering in form_widget and form_label to finally removing form_label.

Upvotes: 3

insertusernamehere
insertusernamehere

Reputation: 23610

You can extend the default form layout using your own twig-file for your fields like this:

<!-- import default layout from symfony -->  
{% use 'form_div_layout.html.twig' with field_label as base_field_label %}

<!-- overwrite the element you want to change (in this case default input-field -->
{% block field_row %}
    {% spaceless %}
        <div class="row">
            <!-- removing this line, you're nearly done -->
            {{ form_label(form) }}
            {{ form_widget(form) }}
        </div>
    {% endspaceless %}
{% endblock field_row %}

And afterwards you set this new form theme in the twig file which renders the form:

{% form_theme form 'VendorNameBundle:Folder:backend_fields.html.twig' %}

That's pretty much it.

If you want to know, what are all the default values, take a look at this file in the repository: form_div_layout.html.twig

Upvotes: 7

Related Questions