2

I've implemented the help messages in forms as in the documentation

{% extends 'form_div_layout.html.twig' %}

{% block field_widget %}
    {{ parent() }}
    {% if help is defined %}
        <span class="help">{{ help }}</span>
    {% endif %}
{% endblock %}

To render the help section, the example says that 'help' should be defined in the twig file as in:

{{ form_widget(form.title, {'help': 'foobar'}) }}

However, I'd like to define 'help' as an option for the fields in the form builder, like this:

class myClassType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('title',null,array('help'=>'foobar'));
    }
}

This has yield no results ("The option "help" does not exist "). I've also tried

$builder
   ->add('title',null,array('attr'=>array(help'=>'foobar')));

and it doesn't work either.

Any ideas to get something like this working?

3 Answers 3

4

Use

$builder
->add('title',null,array('attr'=>array('help'=>'foobar'))); 

And then

{% if attr['help'] is defined %}
    <span class="help">{{ attr['help'] }}</span>
{% endif %}
2
  • 1
    One small downside is that input fields will also have help='foobar' set on it which is redundant IMHO. Aug 29, 2012 at 21:41
  • Thanks I was looking for a way to pass custom parameters to a custom twig block. I did it through attr['help'] as you suggest here. stackoverflow.com/questions/17533159/…
    – svassr
    Jul 10, 2013 at 14:23
2

You can utilize finishView method of AbstractType which ultimately implements FormTypeInterface. e.g,

// Vendor/YourBundle/Form/Type/YourFormType

namespace Vendor\YourBundle\Form\Type;

// other use definitions
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\AbstractType;

class YourFormType extends AbstractType 
{
    // other methods.... 

    /**
     * {@inheritdoc}
     */
    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        parent::finishView($view, $form, $options);
        $view['title']->vars['help'] = "Title help message";
        // same for other fields
    }
}
0

My guess is that the reason you cannot do this is that form field help text is largely a view/templating concern. I realize this does not answer your question entirely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.