我在symfony2中有一个表单,我在其中添加一个复选框,让我们说"条款和条件"需要检查。我想添加一个链接到"条款和条件"我希望将其作为参数传递的标签,让我们说像这样的%url%
messages.en.yml
site.terms: Hiermit akzeptiere ich die <a href="%url%">AGB</a>
在Form类型中,我有类似的东西
$builder
...
->add('terms', 'checkbox', [
'constraints' => [
new True()
],
'label' => 'site.terms'
]);
所以问题是如何将url参数甚至参数数组添加到表单标签中。
为了避免转义表单标签中的HTML,我已经覆盖了块标签,如下所示:
{% block form_label %}
{% if not compound %}
{% set label_attr = label_attr|merge({'for': id}) %}
{% endif %}
{% if required %}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{% endif %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
{# added raw to avoid HTML Escaping #}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain)|raw }}</label>
{% endblock form_label %}
答案 0 :(得分:1)
我遇到了同样的问题。
我没有时间仔细研究更改标签呈现和使用表单字段的附加参数作为转换参数的可能性。
但是,我的快速修复是手动渲染标签并通过将其作为参数传递来更改标签:
{# custom twig extention to get the url for a page #}
{% set url = get_path_by_internal_name('tos', app.request.locale) %}
{{ form_label(terms, 'site.terms'|trans({'%url%': url})|raw) }}