标签选择形式的HTML

问题描述 投票:2回答:1

choiceType字段中,我有参数-选择标签

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

以及树枝中字段的渲染功能:

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

当然,我想用粗体显示getName,我尝试用树枝autoescape{{form_widget(field)|raw}}-没有成功

forms symfony twig html-escape-characters
1个回答
0
投票

您必须在自定义主题中自定义choice_label形式(例如:radioHTML.html.twig ==>我只是在标签文本中添加了| raw)

{%- 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 -%}

您的表单类型只是在choice_label选项中返回html字符串:

 $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)

然后将其用于您的特定领域,例如您的树枝TPL:

{% form_theme from.fieldNalme 'form/radioHTML.html.twig' %}
© www.soinside.com 2019 - 2024. All rights reserved.