Symfony2: collection form field type with data-prototype

前端 未结 1 2013
一个人的身影
一个人的身影 2020-12-07 17:29

I have a Team to which I would like to add players (objects of Player). I\'ve set up the form \"type\" classes correctly. Here\'s the relevant part of my view:



        
1条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 17:48

    This was bugging me too. I customized a field type and even made simple form types with a reduced fieldset (for more complex objects), but it just didn't feel right. I've come up with a solution that should do the trick :)

    First - I think your

    should be outside your loop.

    Next, your prototype is just a form view object, so if you pass it to a template you can render fields by hand and customize the output. Note: autoescape has no effect on included content, so use 'filter escape'.

    Then in your prototypePlayer.html.twig, just render each field as you normally would in a form view.

    {{ form_label(form.name) }} {{ form_row(form.name) }} {{ form_label(form.age) }} {{ form_row(form.age) }} {# render whatever else you like from your Form/PlayerType class... #}

    My template has a different data structure to your question, but it should help :)

    Cheers,

    Chris


    Deprecation Warning:

    As of Symfony 2.1, .get() is deprecated and the above will not work. Replace form.players.get('prototype') with form.players.vars.prototype to fix this in later versions of Symfony.

    0 讨论(0)
提交回复
热议问题