28

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)

j0k
  • 22,600
  • 28
  • 79
  • 90
User Created Image
  • 1,182
  • 2
  • 17
  • 33

5 Answers5

25

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 %}
Nate
  • 1,442
  • 14
  • 22
Alexandre Ouicher
  • 856
  • 1
  • 9
  • 15
19

$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.

zessx
  • 68,042
  • 28
  • 135
  • 158
Henrik Bjørnskov
  • 1,581
  • 1
  • 12
  • 9
  • 2
    That bundle was refactored and the link doesn't work any more. Go here now: https://github.com/simplethings/SimpleThingsFormExtraBundle#helpextension – jmlnik Mar 07 '13 at 18:47
5

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

10us
  • 1,622
  • 18
  • 13
3

You can use the solution in the official docs as you described.

But, the work is not complete yet. You have to create a Form Type Extention, based on this article: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html

After complete the Form Type Extention creation you can add Help Messages like this:

$form = $this->createFormBuilder()
          ->add('name', 'text', array(
                'help' => 'this is a help message to user',
         ))

I think this is a native better solution. Also, i recommend read this great article that shows you how to enable and set the help option in symfony2 forms: http://toni.uebernickel.info/2012/11/03/how-to-extend-form-fields-in-symfony2.1.html

AaronRom
  • 39
  • 3
  • I'm sure the answer is in the links but could you outline the solution from the documentation and the article? – Trudbert Aug 14 '14 at 15:07
2

A little off topic but still useful if you're planning to use Bootstrap for your project then you can take advantage of some form helpers provided by the Mopa Bootstrap Bundle.

Demo: http://bootstrap.mohrenweiserpartner.de/mopa/bootstrap/forms/help_texts

GitHub: https://github.com/phiamo/MopaBootstrapBundle

Example:

<?php

$form = $this->get('form.factory')
        ->createNamedBuilder('form_name')
        ->setMethod('POST')
        ->add('testSelect', 'choice', [
            'choices' => ['val1' => 'Value 1', 'val2' => 'Value 2'],
            'required' => true,
            'help_block' => 'Here some help text!!!'
        ])
        ->add('Save', 'submit')
        ->getForm();

return $form->createView();
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131