Symfony2 How to get the selected value from a select field with twig

☆樱花仙子☆ 提交于 2019-12-06 03:43:09

Hopefully I get you question correctly.

    {% set label = '' %}
    {% for choice in form.proveedor.vars.choices %}
        {% if choice.value == form.proveedor.vars.value %}
            {% set label = choice.label %}
        {% endif %}
    {% endfor %}

    {{ label }}

selected is an attribute, so you can access it with:

{{ dump(form.proveedor.vars.attr["selected"]) }}

Then you can with if sentence check if option has attr equal to selected. If yes then do something, like echo label.

{% if form.proveedor.vars.attr["selected"] == "selected" %}
  {# do something, like echo label #}
{% endif %}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!