How to customize the data-prototype attribute in Symfony 2 forms

后端 未结 11 1177
梦如初夏
梦如初夏 2020-12-12 11:35

Since umpteens days, I block on a problem with Symfony 2 and forms.

I got a form of websites entities. \"Websites\" is a collection of website\'s entities and each w

11条回答
  •  臣服心动
    2020-12-12 12:33

    I know this question is quite old, but I had the same problem and this is how I soved it. I'm using a twig macro to accomplish this. Macros are like functions, you can render them with different arguments.

    {% macro information_prototype(website) %}
        
    {{ form_widget(website.type.code) }}
    {{ form_errors(website.type) }}
    {{ form_widget(website.url) }}
    {{ form_errors(website.url) }}
    {% endmacro %}

    now you can render this macro wherever you want. Note that information_prototype() is just the name of the macro, you can name it whatever you want. If you want to use the macro to render the given items and the prototype the same way, do something like this:

    {% for website in form.websites %} {{ _self.information_prototype(website) }} {% endfor %}

    form.websites.vars.prototype holds the prototype data of the form with the prototype_name you specified. Use _self.+macroname if you want to use the macro in the same template.

    You can find out more about macros in the Twig documentation

提交回复
热议问题