Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 how to remove label for the field using FormBuilder

Tags:

symfony

$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?

like image 212
Mikhail Avatar asked Dec 18 '25 18:12

Mikhail


1 Answers

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

like image 131
insertusernamehere Avatar answered Dec 21 '25 18:12

insertusernamehere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!