Symfony 2.8表单动态生成choice_label raw twig

时间:2015-12-08 14:07:33

标签: php forms symfony twig symfony-2.8

我想渲染原始表单标签,我有以下表单实体类型

    $builder
        ->add('subscriptionBilling', 'entity', array(
            'class'         => 'AppBundle\Entity\SubscriptionBilling',
            'data_class'    => 'AppBundle\Entity\SubscriptionBilling',
            'choice_label' => function ($allChoices, $currentChoiceKey) {
                    return '<p>Categories <strong>'.$allChoices->getNoOfCategories().'  number</strong> '.$currentChoiceKey.'</p>';
            },
            'choices'       => $options['packages_allowed'],
            'data'          => $options['selected_subscriptionBilling'],
            'multiple'      => false,
            'expanded'      => true,
            'required'      => true,
            'label'         => false,
         ))
         ;

和我的树枝

{% autoescape false %}

{{form_label(form_categories.subscription.subscriptionBilling)|raw}}
{{form_widget(form_categories.subscription.subscriptionBilling)|raw}}

{% endautoescape %}

但是我得到了这个HTML

<p>Categories <strong>5 number</strong> 0</p>
<p>Categories <strong>10 number</strong> 1</p>
<p>Categories <strong>25 number</strong> 2</p>
<p>Categories <strong>10 number</strong> 3</p>

1 个答案:

答案 0 :(得分:1)

我终于创建了一个扩展bootstrap表单布局的表单主题

{% extends 'bootstrap_3_layout.html.twig' %}
{% block radio_label %}
    {# Do not display the label if widget is not defined in order to prevent double label rendering #}
    {% if widget is defined %}
        {% if required %}
            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
        {% endif %}
        {% if parent_label_class is defined %}
            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ parent_label_class)|trim}) %}
        {% endif %}
        {% if label is not same as(false) and label is empty %}
            {% set label = name|humanize %}
        {% endif %}
        <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
            {{- widget|raw }} {{ label is not same as(false) ? (translation_domain is same as(false) ?  label|raw : label|trans({}, translation_domain)|raw ) -}}
        </label>
    {% endif %}
{% endblock radio_label %}

和我的树枝

 {% autoescape %}
 {{form_label(form_categories.subscription.subscriptionBilling)}}
 {{form_widget(form_categories.subscription.subscriptionBilling)}}
 {% endautoescape %}

似乎有效