How can I put the asterisk of my required field on my label? (Symfony form)
Asked Answered
D

3

6

I am working on Symfony 3 and I have some trouble with my form.

When I create a Symfony form with a field not required, here is my code :

I create the form :

$form = $this->createFormBuilder()
  ->add('prenom' TextType::class, array(
    'label'    => 'Votre prénom',
    'required' => false
  )
  ->getForm();

Here is the code in my view for this field:

{{ form_label(form.prenom) }}
{{ form_errors(form.prenom) }}
{{ form_widget(form.prenom) }}

And this is the HTML I have :

<label class="control-label" for="contact_prenom">Votre prénom</label>
<input type="text" id="contact_prenom" name="contact[prenom]" class="form-control"/>

Now if I do the same without the 'require' => false on my FormBuilder, here is the HTML I get:

<label class="control-label required" for="contact_prenom">Votre prénom</label>
<sup class="required" title="Champ obligatoire">
        <i class="fa fa-asterisk"></i>
</sup>
<input type="text" id="contact_prenom" name="contact[prenom]" required="required" class="form-control" />

Is it possible to control the "sup" tag so the asterisk * can be with my label?

I guess I can do it with jQuery, but I'd like to know if it is possible to do it on my form builder or in Twig?

Dryfoos answered 18/8, 2017 at 15:14 Comment(0)
C
25

In the doc there is a specific section here http://symfony.com/doc/current/form/form_customization.html#adding-a-required-asterisk-to-field-labels

You can even do with CSS only

label.required:before {
    content: "* ";
}
Cromwell answered 18/8, 2017 at 19:24 Comment(1)
This is what I finaly did, I had the "*" in CSS and delete the "sup" in jQuery. Not the cleanest solution but the main developer told me to use CSS, I'm in intership so I listen ^^Desmarais
Q
2

As of Symfony 5.1 you can do the following

->add('name', TextType::class, [
    'label' => 'Name <span class="badge badge-danger badge-pill">Required</span>',
    'label_html' => true
])

The label_html (bool) property will allow HTML to be injected into the label directly and render on the form output.

Documentation - https://symfony.com/doc/current/reference/forms/types/form.html#label-html

Quaint answered 16/2, 2021 at 22:4 Comment(0)
R
0

Yes, you could override the twig template or the block that symfony uses to render your widget, have a look at: http://symfony.com/doc/current/templating/overriding.html

In your case, you're looking for

vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

And this would be the block you want to override:

{%- block form_label -%}
    {% if label is not same as(false) -%}
        {% 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 -%}
            {%- if label_format is not empty -%}
                {% set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) %}
            {%- else -%}
                {% set label = name|humanize %}
            {%- endif -%}
        {%- endif -%}
        <label{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
    {%- endif -%}
{%- endblock form_label -%}
Respectful answered 18/8, 2017 at 16:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.