Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Form Builder - Remove label, make it placeholder

Tags:

php

symfony

I am playing with Symfony's form builder, and I can't find a way to not display a label. Further, I am interested in actually setting a placeholder for each input box. Is this possible? I have researched a bit and found nothing.

My form:

<form action="{{ path('searchPeople') }}" method="post" class="form-inline">
    {{ form_errors(form) }}
    
    {{ form_row(form.first_name) }}
    {{ form_row(form.last_name) }}
    
    {{ form_rest(form) }}
    
    <br />
    <button type="submit" class="btn btn-primary" /><i class="icon-search"></i>Search</button>
</form>
like image 616
Josh Wa Avatar asked Jun 07 '13 22:06

Josh Wa


3 Answers

I know it's already answered, but might help somebody who is looking for a different solution for placeholders, if you don't want to change anything in your twig template:

$builder->add(
    'name',
    'text', 
     array(
        'attr' => array(
             'placeholder' => 'Your name',
        ),
        'label' => false,
     )
);
like image 116
Titi Avatar answered Nov 03 '22 00:11

Titi


If you're outputting the field with form_rest you'll have to set the label for the the field to false in the form builder with something like

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

If you output the fields individually, you can omit the form_label for that field in the twig template, or set it to an empty string.

{{ form_label(form.first_name, '') }}
like image 20
Fo. Avatar answered Nov 02 '22 23:11

Fo.


Convert label to placeholder

{% use 'form_div_layout.html.twig' with widget_attributes as base_widget_attributes %}
{% block widget_attributes %}
    {% set attr = {'placeholder': label|trans({}, translation_domain)} %}
    {{- block('base_widget_attributes') -}}
{% endblock widget_attributes %}
like image 6
Léo Benoist Avatar answered Nov 02 '22 23:11

Léo Benoist