Adding "help" messages to fields

User Created Image picture User Created Image · Sep 20, 2011 · Viewed 22.3k times · Source

I'm trying to add some help messages after each field in form in symfony2.

I have read about one solution in official docs : http://symfony.com/doc/current/cookbook/form/form_customization.html#adding-help-messages

But this solution makes little sense, because we've need to create all form manually. For example, it easy to define label: $formBuilder->add('myfieldname', 'text', array('label'=>'some my field label')); But how to pass help messages? (In other words, some custom variables)

Answer

Alexandre Ouicher picture Alexandre Ouicher · Aug 22, 2014

A another method without another extension :

In your form builder class:

$builder->add('yourField',null, array('attr'=>array('help'=>'text help')))

In your form template rewrite:

{% block form_row %}
    {% spaceless %}
            {{ form_label(form) }}
                {{ form_widget(form) }}
                {% for attrname, attrvalue in attr %}
                    {% if attrname == 'help' %}
                        <span class="help-block">{{ attrvalue }}</span>
                    {% endif %}
                {% endfor %}
            {{ form_errors(form) }}
    {% endspaceless %}
{% endblock form_row %}