Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link in form label using route

Tags:

symfony

In my registration form i have a checkbox "I accept the terms", and want to link the word "terms" to my terms page.

Is there a way to add a link to a form label, using a route? (preferably without injecting the container in the form)

like image 696
smoove Avatar asked Aug 21 '12 11:08

smoove


2 Answers

As the solution above somehow didn't work for me I solved it using the solution suggested here: https://gist.github.com/marijn/4137467

OK, so here i how I did it:

    {% set terms_link %}<a title="{% trans %}Read the General Terms and Conditions{% endtrans %}" href="{{ path('get_general_terms_and_conditions') }}">{% trans %}General Terms and Conditions{% endtrans %}</a>{% endset %}
{% set general_terms_and_conditions %}{{ 'I have read and accept the %general_terms_and_conditions%.'|trans({ '%general_terms_and_conditions%': terms_link })|raw }}{% endset %}
<div>
{{ form_errors(form.acceptGeneralTermsAndConditions) }}

{{ form_widget(form.acceptGeneralTermsAndConditions) }}
<label for="{{ form.acceptGeneralTermsAndConditions.vars.id }}">{{ general_terms_and_conditions|raw }}</label>
</div>
like image 123
totas Avatar answered Sep 20 '22 01:09

totas


The best way is to overwrite the twig block used to render that specific label.

First, check the form fragment naming section of the docs. Then create a new block in your form template with the the appropriate name. Don't forget to tell twig to use it:

{% form_theme form _self %}

For the next step check the default form_label block.

You'll probably only need a portion of it, something like this (I'm leaving the default block name here):

{% block form_label %}
{% spaceless %}
    <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
        <a href="{{ path("route_for_terms") }}">{{ label|trans({}, translation_domain) }}</a>
    </label>
{% endspaceless %}
{% endblock %}
like image 36
Maerlyn Avatar answered Sep 22 '22 01:09

Maerlyn