Symfony2 / Twig - re-select options

The usual way to display the select field is to call

 {{ form_row(form.doctor_service_id, {'attr':{'class':'form-control'}}) }} 

I would like to accomplish two things:

  • Check if this field is really a selection field.
  • Iterate over each option (value, name). I know how the twig iterator works, I just don't know how to access the select parameters and apply them to it.
+5
source share
2 answers
 <select name="country"data-width="100%"> {% for key,val in form.country.vars.choices %} <option value="{{ val.value }}" {{ form.country.vars.value == '' and key == 0 ? ' selected ' :(val.value == form.country.vars.value ? ' selected ' : '') }}>{{ val.label | trans }}</option> {% endfor %} </select> 
+12
source
  • You can use the branch extension to add the instanceof operator. If you are new to creating twig extensions, see How to Write a Custom Twig Extension in Symfony Documents. There is an entity that gives an example of a Twig extension that implements the instanceof operator . Then, to check if the field is used in the selection field:

      {% if value is instanceof('ChoiceType') %} 
  • This is not as easy as you think, because there are so many options in the selection fields. With Symfony, this is a form theme that defines how various types of fields are displayed in html. The default form theme is in form_div_layout.html.twig . About 50 lines of code are required to display the selection field, taking into account all the options covered by the choice_widget, choice_widget_expanded, choice_widget_collapsed and choice_widget_options . You can select the bits you need based on the parameters that you set for your selection field and paste them into your branch template, but then setting options in the form class will not affect. The right way to personalize your choices (* provided that the choices are not expanded) is to override the choice_widget_options block from the form theme. "Customizing the form" is a topic in itself, but the easiest way is to redefine the block as a one-time template for your branch and then change it to fit your needs, for example

     {% extends '::base.html.twig' %} {% form_theme form _self %} {%- block choice_widget_options -%} {% for group_label, choice in options %} {%- if choice is iterable -%} <optgroup label="{{ choice_translation_domain is sameas(false) ? group_label : group_label|trans({}, choice_translation_domain) }}"> {% set options = choice %} {{- block('choice_widget_options') -}} </optgroup> {%- else -%} {% set attr = choice.attr %} <option value="{{ choice.value }}" {{ block('attributes') }}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is sameas(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option> {%- endif -%} {% endfor %} {%- endblock choice_widget_options -%} {% block content %} {# ... render the form #} {{ form_row(form.doctor_service_id, {'attr':{'class':'form-control'}}) }} {% endblock %} 

If you do not need to customize the rendering of your selection field, but just want the data to do something else with it, it is best to transfer the data (which is displayed in the selection field) so that the branch template along with the form and use it directly. If this is not an option, you can repeat the choice, as in the form topic, although you may need to consider your preferred options. The simplest case would be, for example,

 {% for choice in form.doctor_service_id.vars.choices %} {{ choice.label }} {{ choice.value }} {% endfor %} 
+2
source

All Articles