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)
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 %}
$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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With