添加“帮助”到字段的消息

发布于 2024-12-06 02:39:16 字数 441 浏览 2 评论 0原文

我试图在 symfony2 中的表单中的每个字段后面添加一些帮助消息。

我在官方文档中读到了一种解决方案: http://symfony.com/ doc/current/cookbook/form/form_customization.html#adding-help-messages

但是这个解决方案没有什么意义,因为我们需要手动创建所有表单。 例如,定义标签很容易: $formBuilder->add('myfieldname', 'text', array('label'=>'some my field label')); 但是如何传递帮助消息? (也就是说,一些自定义变量)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怕倦 2024-12-13 02:39:17

有点偏离主题,但如果您计划在项目中使用 Bootstrap,那么您可以利用 Mopa Bootstrap Bundle 提供的一些表单助手,但仍然有用。

演示:http://bootstrap.mohrenweiserpartner.de/mopa/bootstrap/forms/help_texts< /a>

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

示例:

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

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();
被你宠の有点坏 2024-12-13 02:39:16

没有其他扩展的另一种方法:

在您的表单构建器类中:

$builder->add('yourField',null, array('attr'=>array('help'=>'text help')))

在您的表单模板重写中:

{% 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 %}

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 %}
高速公鹿 2024-12-13 02:39:16

$formBuilder->add('myFieldName', 'text', array('help' => 'My Help Message')); 但它认为您还需要添加一个扩展来添加这是所有表单的默认选项:
https://github.com/simplethings/SimpleThingsFormExtraBundle#helpextension
这使您能够直接从 FormType 编辑属性。

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

暗藏城府 2024-12-13 02:39:16

从 symfony 4.1 开始,您可以执行以下操作:

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

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

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

北座城市 2024-12-13 02:39:16

您可以按照您的描述使用官方文档中的解决方案。

但是,工作还没有完成。您必须根据本文创建表单类型扩展: http://symfony .com/doc/current/cookbook/form/create_form_type_extension.html

完成表单类型扩展创建后,您可以添加如下帮助消息:

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

我认为这是一个原生的更好的解决方案。
另外,我建议阅读这篇精彩的文章,它向您展示了如何在 symfony2 表单中启用和设置帮助选项:
http://toni .uebernickel.info/2012/11/03/how-to-extend-form-fields-in-symfony2.1.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文