Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding "help" messages to fields

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)

like image 580
User Created Image Avatar asked Sep 20 '11 14:09

User Created Image


3 Answers

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 %} 
like image 93
Alexandre Ouicher Avatar answered Oct 14 '22 16:10

Alexandre Ouicher


$formBuilder->add('myFieldName', 'text', array('help' => 'My Help Message')); But it think you also need to add an extension that add this as a default option for all forms :
https://github.com/simplethings/SimpleThingsFormExtraBundle#helpextension
This makes you able to edit attributes directly from you FormTypes.

like image 34
Henrik Bjørnskov Avatar answered Oct 14 '22 17:10

Henrik Bjørnskov


Since symfony 4.1 you can do :

$builder->add('email', null, [
    'help' => 'Make sure to add a valid email',
]);

https://symfony.com/blog/new-in-symfony-4-1-form-field-help

like image 25
10us Avatar answered Oct 14 '22 16:10

10us