Html in label choice form
Asked Answered
S

2

5

In choiceType field I have parameter - choice label

   'choice_label' => function ($pay, $key, $index) {
        return "<b>".$pay->getName()."</b><br />";
    },

And render function of fields in twig:

{{form_widget(field)}}
{{form_errors(field)}}

Of course I want to render getName in bold characters, I have try with twig autoescape and {{form_widget(field)|raw}} - without success

Strangle answered 8/8, 2017 at 7:39 Comment(4)
Try return new \Twig_Markup("<b>".$pay->getName()."</b><br />", "UTF-8");Nicolnicola
@Nicolnicola does not workStrangle
AFAIK html tag for <option> content is not support by browsers. Try with css styles.Cinder
It is not <option> I rendered choiceType as radio button, so choice_label is in <label>Strangle
F
6

Since Symfony 5.1 'label_html' => true can be set to allow HTML in labels.

Note that this does not work with 'expanded' => false, as this renders a HTML select box (which per its nature does not support HTML in the value label).

$builder->add('mychoices', ChoiceType::class, [
    'required' => true,
    'expanded' => true,
    'choices' => ['Test 1', 'Test 2'],
    'label_html' => true,
    'choice_label' => function ($label) {
        return '<strong>' . $label . '</strong>';
    },
]);
Farro answered 10/4, 2023 at 9:14 Comment(3)
It seems it doesn't work on choice labels.List
choice_label works for me. I adapted my use case and added an example above? I'm using radio buttons here. Not sure if that makes a difference with checkboxes.Farro
I think I understood. It works with radios, but not with a select (expanded → false). @see github.com/symfony/symfony/issues/40806List
R
2

You have to custom the choice_label form in a custom theme (ex : radioHTML.html.twig) => I just add |raw to the label text

{%- 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 -%}
    <{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
    {%- if translation_domain is same as(false) -%}
        {{- label|raw -}}
    {%- else -%}
        {{- label|trans({}, translation_domain)|raw -}}
    {%- endif -%}
    </{{ element|default('label') }}>
{%- endif -%}
{%- endblock form_label -%}

Your form type just return html string in the choice_label option :

 $builder->add('fieldName', EntityType::class, array(
            'required' => true,
            'class' => 'AppBundle\EntityName',
            'choice_label' => function (EntityName $entity) {
                $html = '<div>';
                $html .= $entity->getName();
                $html .= '</div>';
                return $html;
            },
            'data' => null,
            'multiple' => false,
            'expanded' => true)

And then use it for your specific field like that in your twig TPL :

{% form_theme from.fieldNalme 'form/radioHTML.html.twig' %}
Remmer answered 20/11, 2019 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.