Symfony FormBuilder - 附加元素的自定义选项?

Symfony FormBuilder - custom option for additional element?

我最近才开始使用 Symfony (Symfony3),并且正在将我的旧网站转换为使用 Symfony。 目前,在我的表单元素中,我添加了一个验证错误消息元素,以及一个 help/information 元素(一个在单击时显示文本的信息符号)。

<div id="info-username" class="additional-info">
    <p>Your username will be used as a way of <em>uniquely</em> identifying you when you log into your account. You can still log in using your email address in case you forget your username.</p>
    <p>It must be between 2-25 characters long and start with a letter. The following characters are permitted: letter, number, dot, underscore, dash.</p>
</div>

Symfony 按照标准处理验证错误消息元素,但我看不到轻松添加额外信息文本的方法。理想情况下,我希望这是一个额外的选项,然后我可以将其传递给 FormBuilder 的 'add' 方法。例如

->add('username', TextType::class, array(
    'info' => '<p>Your username will be used as a way of <em>uniquely</em> identifying you when you log into your account. You can still log in using your email address in case you forget your username.</p>
<p>It must be between 2-25 characters long and start with a letter. The following characters are permitted: letter, number, dot, underscore, dash.</p>'
)

这可能吗?而不是直接将它添加到我的树枝模板中。

尝试以下操作:

->add('username', TextType::class, array(
    'help' => '<p>Your username will be used as a way of <em>uniquely</em> identifying you when you log into your account. You can still log in using your email address in case you forget your username.</p>
<p>It must be between 2-25 characters long and start with a letter. The following characters are permitted: letter, number, dot, underscore, dash.</p>'
)

如果它不适用于此字段类型,请查看 the following form extension

我已经找到了自己的解决方案...

我在必填字段中添加了 'help' 属性。例如

->add('username', TextType::class, array(
    'attr' => array(
        'help' => '<p>Your username will be used as a way of <em>uniquely</em> identifying you when you log into your account. You can still log in using your email address in case you forget your username.</p>
<p>It must be between 2-25 characters long and start with a letter. The following characters are permitted: letter, number, dot, underscore, dash.</p>'
    ),
    )
)

然后我不得不覆盖 form_row 块以添加我的 help/info 框和内容。请注意 'raw' 过滤器的使用 - 我需要它,因为我的帮助文本包含需要呈现的 html 标签,因此使用此过滤器不会转义标签。

{# \app\Resources\views\form\form_div_layout.html.twig #}
{% block form_row %}
  {% spaceless %}
    <div class="form-group">
      {{ form_label(form) }}
      <div class="col-sm-10">
        {{ form_widget(form) }}
        {{ form_errors(form) }}
        {% for attrname,attrvalue in attr %}
          {% if attrname == 'help' %}
            <div id="info-{{ id }}" class="additional-info alert alert-info alert-dismissible">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              {{ attrvalue | raw }}
            </div>
          {% endif %}
        {% endfor %}
      </div>
    </div>
  {% endspaceless %}
{% endblock form_row %}

这使信息框正确显示。但是,它将原始文本留在字段的 'help' 属性中,因此我通过覆盖 widget_attributes 块删除了它并忽略了输出 'help' 属性。

{# \app\Resources\views\form\form_div_layout.html.twig #}
{%- block widget_attributes -%}
    id="{{ id }}" name="{{ full_name }}"
    {%- if disabled %} disabled="disabled"{% endif -%}
    {%- if required %} required="required"{% endif -%}
    {%- for attrname, attrvalue in attr -%}
        {%- if attrname == 'help' -%}
          {# Prevent output of the help attribute #}
        {%- else -%}
          {{- " " -}}
          {%- if attrname in ['placeholder', 'title'] -%}
              {{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
          {%- elseif attrvalue is same as(true) -%}
              {{- attrname }}="{{ attrname }}"
          {%- elseif attrvalue is not same as(false) -%}
              {{- attrname }}="{{ attrvalue }}"
          {%- endif -%}
        {%- endif -%}
    {%- endfor -%}
{%- endblock widget_attributes -%}