我最近才开始使用Symfony(Symfony3),而我正在将旧网站转换为使用Symfony。 目前使用我的表单元素,我添加了一个验证错误消息元素,以及一个帮助/信息元素(一个显示单击时文本的信息符号)。
<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'方法。 E.g。
->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>'
)
这可能吗?而不是直接在我的树枝模板中添加它。
答案 0 :(得分:1)
尝试以下方法:
->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
答案 1 :(得分:0)
我已经找到了自己的解决方案......
我在必填字段中添加了'help'属性。 E.g。
->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块以添加我的帮助/信息框和内容。请注意使用'原始'过滤器 - 我需要这个,因为我的帮助文本包含需要渲染的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">×</span></button>
{{ attrvalue | raw }}
</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endspaceless %}
{% endblock form_row %}
这使信息框显示正确。但是,它将原始文本保留在字段的“帮助”属性中,因此我通过覆盖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 -%}