开发者

Adding "help" messages to fields

开发者 https://www.devze.com 2023-04-05 23:23 出处:网络
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#addi

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


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


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号