Link to form label using route

In my registration form, I have the checkbox “I accept the terms” and want to link the word “terms” with my terms page.

Is there a way to add a link to a form label using a route? (preferably without injection of the container in the mold)

+6
source share
5 answers

The best way is to rewrite the branch block used to render this particular label.

First, check the section of the document with the names of the document fragments. Then create a new block in the form template with the appropriate name. Remember to tell the branch to use it:

{% form_theme form _self %} 

For the next step, check the default form_label .

You probably only need some of this, something like this (I leave 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 %} 
+4
source

Since the solution above does not work for me for some reason, I solved it using the solution proposed here: https://gist.github.com/marijn/4137467

Ok, here I am, as 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> 
+6
source

My solution was different:

the form:

 $builder ->add( 'agree_to_rules', 'checkbox', [ 'required' => true, 'label' => 'i_agree_to' ] ); 

And html:

 <span style="display:inline-block"> {{ form_widget(form.agree_to_rules) }} </span> <span style="display:inline-block"> <a href="#">rules</a> </span> 

And it looks the same :)

0
source

A very easy way to do this would be

 {{ form_widget(form.terms, { 'label': 'I accept the <a href="'~path('route_to_terms')~'">terms and conditions</a>' }) }} 

You can also do this if you want to use the translation.

In your translation file, for example message.en.yml add

 terms: url: 'I accept the <a href="%url%">terms and conditions</a>' 

And, in your opinion, add

 {{ form_widget(form.terms, { 'label': 'terms.url'|trans({'%url%': path('route_to_terms')}) }) }} 
0
source

Alternatively, you can do this:

 ->add('approve', CheckboxType::class, [ 'label' => 'Text part without link', 'help' => 'And <a href="/x.pdf">download it</a>', 'help_html' => true, ]) 
0
source

Source: https://habr.com/ru/post/923376/


All Articles