Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a translation with variable for a Bootstrap checkbox in Symfony

In a Symfony 4 project, I use Bootstrap theme for rendering my HTML stuff.

I have a form which contains a checkbox for accepting terms like:

☑ I accept terms

I want to get the link as a variable (will change depending of language)

So I have a translation yaml like this :

form:
  register:
    title: Registration
    username: Username
    email: Email
    password: Password
    repeat_password: Repeat your password
    accept_terms: Accept %terms%

In my formType file, I can't inject the translation because I found nothing to feed the %terms% parameter.

->add(
    'termsAccepted',
    CheckboxType::class,
    [
        'mapped' => false,
        'constraints' => new IsTrue(),
        'label' => 'form.register.accept_terms',
    ]
)

In twig file I can't change the form_label because... in Symfony documentation, it says that form_label doesn't work for checkbox (and radio button)

{{ form_label(form.termsAccepted, 'that custom label is ignored...') }}

Any idea on how to send a translation (with parameter) on a checkbox element (Bootstrap 4)?

SOLUTION

In my form twig :

{% form_theme form.termsAccepted _self %}

{% block checkbox_label -%}
    ... here is a copy/paste of the orignial code from https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig ...

        {{ widget|raw }}
        <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
        {% set terms_link = '<a href="http://url.com">'~'form.register.terms_link'|trans()~'</a>' %}
        {{- label is not same as(false) ? (translation_domain is same as(false) ? label|raw : label|trans({'%terms%': terms_link}, translation_domain)|raw) -}}
        {{- form_errors(form) -}}
        </label>
    {%- endif -%} {%- endblock checkbox_label %}
like image 933
shabang Avatar asked Dec 07 '25 10:12

shabang


1 Answers

In your view you could insert, at the top, this

{% form_theme form.termsAccepted _self %}

as written here in the docs https://symfony.com/doc/current/form/form_customization.html#child-forms

And then you could overwrite the checkbox_label shipped in bootstrap 4 form theme with the one you need. Just write

{% block checkbox_label %}
    Copy and paste here the same block from bootstrap 4 form them and edit as your needs. See https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig
{% endblock %}

This custom block will be used only for form.termsAccepted.

If you want a more generic solution you can create your own type (e.g. CheckboxlinkType that extends CheckboxType, add the needed options (linkUri and linkText) in configureOptions and pass their value to the view in buildView. Then you can write the specific block you need to render your type, called checkboxlink_label using the one in bootstrap 4 form theme as base and using your variables. Let me know if you need help with this.

like image 128
ste Avatar answered Dec 11 '25 13:12

ste