Symfony2表单实体样式每个选项不同(折叠)

Symfony2表单实体样式每个选项不同(折叠),symfony,symfony-forms,Symfony,Symfony Forms,我在表单中有一个实体字段访客。我想根据一个人的身份来强调他 ->add('visitors', 'entity', array( 'class' => "T2m3Bundle:Person", 'multiple' => true, 'property' => 'entireName', 'required' => false, ) 我有一个方法getStatus,它返回4个状态。在每个州,我都想在选项标签中加

我在表单中有一个实体字段访客。我想根据一个人的身份来强调他

->add('visitors', 'entity', array(
     'class'     => "T2m3Bundle:Person",
     'multiple'  => true,
     'property'  => 'entireName',
     'required'  => false,
)
我有一个方法getStatus,它返回4个状态。在每个州,我都想在选项标签中加入不同的样式

以下是一个例子:

<select>
  <option value="1">Mary</option>
  <option value="2" class="alert alert-danger">John</option>
  <option value="3" class="alert alert-success">Peter</option>
  <option value="4" class="alert alert-warning">Robert</option>
</select>

但是我不知道如何手动呈现折叠类型,其中没有{{form_widget…-}}

尝试覆盖细枝选择小部件:参考以查找相应的块。是这样的

{% block choice_widget_collapsed -%}
    {% if required and placeholder is none and not placeholder_in_choices and not multiple -%}
        {% set required = false %}
    {%- endif -%}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
        {% if placeholder is not none -%}
            <option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder|trans({}, translation_domain) }}</option>
        {%- endif %}
        {%- if preferred_choices|length > 0 -%}
            {% set options = preferred_choices %}
            {{- block('choice_widget_options') -}}
            {% if choices|length > 0 and separator is not none -%}
                <option disabled="disabled">{{ separator }}</option>
            {%- endif %}
        {%- endif -%}
        {% set options = choices -%}
        {{- block('choice_widget_options') -}}
    </select>
{%- endblock choice_widget_collapsed %}

{% block choice_widget_options -%}
    {% for group_label, choice in options %}
        {%- if choice is iterable -%}
            <optgroup label="{{ group_label|trans({}, translation_domain) }}">
                {% set options = choice %}
                {{- block('choice_widget_options') -}}
            </optgroup>
        {%- else -%}
            {# HERE we define the class based on the status #}
            {% if status is defined %}
                {% if status == 1 %}
                    {% set status_class = 'alert alert-warning' %}
                {% elseif status == 2 %}
                    {% set status_class = 'alert alert-danger' %}
                {% elseif ... %}
                    ....
                {% endif %}
            {% endif %}
            <option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %} {% if status_class is defined %} class="{{ status_class }}" {% endif %} >{{ choice.label|trans({}, translation_domain) }}</option>
        {%- endif -%}
    {% endfor %}
{%- endblock choice_widget_options %}

这是没有测试,但应该工作。让我知道

谢谢你的回答。逻辑对我来说似乎很好,但我有一个问题,表单没有呈现某些内容。它似乎忽略了{{form_widgetvisitor,{'status':entity.status}},因为模板中没有小部件。是否有可能呈现每个选项?您需要将我给您的代码放入模板中!并将此模板称为细枝主题!请参阅此链接,以了解我所做的一切。导入的模板在我的视图中带有{%form\u主题表单'MyBundle:form:form\u visitors\u availability.html.twig%}。清除了一个缓存,所有的东西。主题很好,但是我在那里找不到form_小部件。但是模板也没有问题,因为否则它应该在我在答案中编写的代码中工作。
{% block choice_widget_expanded -%}
<div {{ block('widget_container_attributes') }}>
{%- for child in form %}
    {{- form_widget(child) -}}
    {{- form_label(child) -}}
{% endfor -%}
</div>
{% endblock choice_widget_expanded %}
{% block choice_widget_collapsed -%}
    {% if required and placeholder is none and not placeholder_in_choices and not multiple -%}
        {% set required = false %}
    {%- endif -%}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
        {% if placeholder is not none -%}
            <option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder|trans({}, translation_domain) }}</option>
        {%- endif %}
        {%- if preferred_choices|length > 0 -%}
            {% set options = preferred_choices %}
            {{- block('choice_widget_options') -}}
            {% if choices|length > 0 and separator is not none -%}
                <option disabled="disabled">{{ separator }}</option>
            {%- endif %}
        {%- endif -%}
        {% set options = choices -%}
        {{- block('choice_widget_options') -}}
    </select>
{%- endblock choice_widget_collapsed %}

{% block choice_widget_options -%}
    {% for group_label, choice in options %}
        {%- if choice is iterable -%}
            <optgroup label="{{ group_label|trans({}, translation_domain) }}">
                {% set options = choice %}
                {{- block('choice_widget_options') -}}
            </optgroup>
        {%- else -%}
            {# HERE we define the class based on the status #}
            {% if status is defined %}
                {% if status == 1 %}
                    {% set status_class = 'alert alert-warning' %}
                {% elseif status == 2 %}
                    {% set status_class = 'alert alert-danger' %}
                {% elseif ... %}
                    ....
                {% endif %}
            {% endif %}
            <option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %} {% if status_class is defined %} class="{{ status_class }}" {% endif %} >{{ choice.label|trans({}, translation_domain) }}</option>
        {%- endif -%}
    {% endfor %}
{%- endblock choice_widget_options %}
{% for visitor in form.visitors %}
    {% set index = visitor.vars.value %}
    {% set entity = form.visitors.vars.choices[index].data %}

        {{ form_widget(visitor, {'status': entity.status}) }}
        ....
{% endfor %}