Reputation: 1506
I have a form in symfony2 where I add a checkbox for let's say "Terms and Conditions" that needs to be checked. I want to add a link to "Terms and Conditions" label which I want to pass it as a parameter, let's say %url% like this
messages.en.yml
site.terms: Hiermit akzeptiere ich die <a href="%url%">AGB</a>
In the Form type I have something like this
$builder
...
->add('terms', 'checkbox', [
'constraints' => [
new True()
],
'label' => 'site.terms'
]);
So the question is how can I add that url parameter or even an array of parameters to the form label.
For avoiding escaping the HTML inside the form label I have overwritten the block label like this:
{% 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 %}
Upvotes: 2
Views: 2475
Reputation: 2619
I had the same problem.
I didn't have the time to look deeply into the possibility of changing the label rendering and using a added argument to the form field as the translation parameters.
However, my quick-fix was to render the label manually and change the label by passing it as an argument:
{# 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) }}
Upvotes: 1