Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

php - Symfony2 Form Builder - Remove label, make it placeholder

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>

EDIT: Solved! All of the solutions below helped, but I gave the answer to the primary helpful comment. I appreciate all of the help. For anyone else that comes across this, this is my final working code:

<form action="{{ path('searchPeople') }}" method="post" class="form-inline">
    {{ form_errors(form) }}

    {{ form_errors(form.first_name) }}
    {{ form_widget(form.first_name, {'attr': {'placeholder': 'First Name'} }) }}

    {{ form_errors(form.last_name) }}
    {{ form_widget(form.last_name, {'attr': {'placeholder': 'Last Name'} }) }}

    {{ form_rest(form) }}

    <br />
    <button type="submit" class="btn btn-primary" /><i class="icon-search icon-white"></i>Search</button>
</form>
question from:https://stackoverflow.com/questions/16993684/symfony2-form-builder-remove-label-make-it-placeholder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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,
     )
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...