How to hide form fields in twig template?

﹥>﹥吖頭↗ 提交于 2019-12-01 18:03:58

you can use HiddenType, or hide field in template:

{{ form_start(form) }}
    {% if someValue == true %}
        {{ form_widget(form.fieldName) }}
    {% else %}
        {{ form_widget(form.fieldName, { 'attr': {'class': 'hidden-row'} }) }}
    {% endif %}
    {# other fields... #}
{{ form_end(form) }}

or you can use FormEvents like FormEvents::PRE_SET_DATA in FormType. (doc)

You can prevent any output for the form field by pretending, that it was already rendered:

{{ form_start(form) }}
    {% if someValue == true %}
        {% do form.fieldName.setRendered() %}
    {% endif %}
{{ form_end(form) }}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!