2013-10-22 46 views
0

我有問題。我想選擇與國旗ico國家,但我不知道如何創建自定義主題的形式選擇提交。Symfony 2形式的主題與選擇字段

我創建的測試形式:

->add('name', 'choice', array('choices' =>array('en' => 'England', 'de' => 'Deutshland'))) 

下一頁在我看來,我嘗試

{% block _send_name_widget %} 
    <select> 
     {% for f in form %} 
      {{ loop.index }} 
     {%endfor%} 
    </select> 
{% endblock%} 

{{ form_widget(form.name) }} 

而在我的HTML代碼,我有:

<select> 
1 
2 
</select> 

<select> 
</select> 

你能告訴我爲什麼? 如何僅使用參數渲染一個選擇?

回答

1

替代選擇模板:關於形式的主題化

{% block choice_widget_options %} 
{% spaceless %} 
    {% for group_label, choice in choices %} 
     {% if choice is iterable %} 
      <optgroup label="{{ group_label|trans({}, translation_domain) }}"> 
       {% set options = choice %} 
       {{ block('choice_widget_options') }} 
      </optgroup> 
     {% else %} 
      <option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}> 
        <img src="/images/flags/{{ choice.label }}.jpg" /> 
        {{ choice.label|trans({}, translation_domain) }} 
      </option> 
     {% endif %} 
    {% endfor %} 
{% endspaceless %} 
{% endblock choice_widget_options %} 

更多信息在Symfony2 docs

+1

感謝您的快速答覆。 我用你的例子,但我錯了: 變量「選項」不存在 – Micchaleq

+0

它存在。仔細閱讀文檔並實現像那裏的主題 – Udan

+1

嗨, 我使用那裏的示例 https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form /form_div_layout.html.twig 和我閱讀文檔,但我總是有錯誤。 {%爲GROUP_LABEL,選擇在選項%} 變量「選項」不存在 我不知道我應該在哪裏尋找錯誤。 有人可以幫助我嗎? – Micchaleq

3

變量「選項」,在我choice_widget_options模板不存在。事實是,「選項」不是變量的正確名稱。如果你看看\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\views\Form\choice_widget_options.html.php,你會發現Symfony使用一個叫做'choices'的變量。

以前的代碼的修正版本是:

{% block choice_widget_options %} 
{% spaceless %} 
    {% for group_label, choice in choices %} 
     {% if choice is iterable %} 
      <optgroup label="{{ group_label|trans({}, translation_domain) }}"> 
       {% set options = choice %} 
       {{ block('choice_widget_options') }} 
      </optgroup> 
     {% else %} 
      <option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}> 
       <img src="/images/flags/{{ choice.label }}.jpg" /> 
       {{ choice.label|trans({}, translation_domain) }} 
     </option> 
    {% endif %} 
{% endfor %} 
{% endspaceless %} 
{% endblock choice_widget_options %}