Reputation: 433
I have problem. I want to do choices country with flag ico but I don't know how to create custom theme for form choice filed.
I created test form :
->add('name', 'choice', array('choices' =>array('en' => 'England', 'de' => 'Deutshland')))
Next in my view I try
{% block _send_name_widget %}
<select>
{% for f in form %}
{{ loop.index }}
{%endfor%}
</select>
{% endblock%}
{{ form_widget(form.name) }}
And in my html code I've got:
<select>
1
2
</select>
<select>
</select>
Could you tell me why? How can I render only one select with parameters?
Upvotes: 0
Views: 8172
Reputation: 2210
The variable "options" didn't exist in my choice_widget_options template. The fact is that "options" IS NOT the correct name of the variable. If you take a look at \vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\views\Form\choice_widget_options.html.php
, you'll find Symfony use a variable called 'choices'.
The corrected version of the previous code is :
{% 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 %}
Upvotes: 3
Reputation: 5599
Override the choice template:
{% 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 %}
more info about form theming in the Symfony2 docs
Upvotes: 1