How to handle multiple nested form collections in Symfony 2.0?

I have an extension to this question: How do I work with a collection of forms in Symfony2 beta? - My project is similar, but the objects are nested deeper. I have articles containing one or more content elements, each of which contains one or more media. While the model and the controllers are working fine, but I do not know how to properly represent the nesting in my template. The form /ContentType.php looks right:

class ContentType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('headline') ->add('text') ->add('medias', 'collection', array( 'type' => new MediaType(), 'allow_add' => true )) ; } 

And until now, the form template for creating (or editing) an article looks like this (almost a vanilla auto-generated template):

 ... <form action="{{ path('article_create') }}" method="post" {{ form_enctype(form) }}> {{ form_widget(form) }} {% for content in form.contents %} {{ form_widget(content) }} {% endfor %} <p> <button type="submit">Create</button> </p> </form> ... 

How can I access each Media Media content so that it is correctly associated?

+7
source share
2 answers

Iterate through all media content:

 <form action="{{ path('article_create') }}" method="post" {{ form_enctype(form) }}> {{ form_widget(form) }} {% for content in form.contents %} {% for media in content.medias %} {{ form_widget(media) }} {% endfor %} {% endfor %} <p> <button type="submit">Create</button> </p> </form> 
+2
source
 <form action="{{ path('article_create') }}" method="post" {{ form_enctype(form) }}> {% for media in form.contents.medias.children %} {{ form_widget(media) }} {% endfor %} {{ form_rest(form) }} <p> <button type="submit">Create</button> </p> </form> 
-one
source

All Articles