Browse Source

[Form] Make the prototype view child of the collection view

Victor Berchet 14 years ago
parent
commit
4e3e2768fb

+ 3 - 6
src/Symfony/Bridge/Twig/Resources/views/Form/div_layout.html.twig

@@ -260,11 +260,8 @@
 {% endspaceless %}
 {% endblock email_widget %}
 
-{% block collection_widget %}
+{% block prototype_row %}
 {% spaceless %}
-    {{ block('form_widget') }}
-    {% if prototype is defined %}
-    <script type="text/html" id="{{ id }}_prototype">{{ form_row(prototype) }}</script>
-    {% endif %}
+    <script type="text/html" id="{{ proto_id }}">{{ block('field_row') }}</script>
 {% endspaceless %}
-{% endblock collection_widget %}
+{% endblock prototype_row %}

+ 0 - 5
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/collection_widget.html.php

@@ -1,5 +0,0 @@
-<?php echo $view->render('FrameworkBundle:Form:form_widget.html.php', array('form' => $form)) ?>
-
-<?php if (isset($prototype)): ?>
-<script type="text/html" id="<?php echo $view->escape($id) ?>_prototype"><?php echo $view['form']->row($prototype) ?></script>
-<?php endif; ?>

+ 3 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/prototype_row.html.php

@@ -0,0 +1,3 @@
+<script type="text/html" id="<?php echo $view->escape($proto_id) ?>">
+    <?php echo $view->render('FrameworkBundle:Form:field_row.html.php', array('form' => $form)) ?>
+</script>

+ 2 - 2
src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php

@@ -26,7 +26,7 @@ class CollectionType extends AbstractType
     {
         if ($options['allow_add'] && $options['prototype']) {
             $prototype = $builder->create('$$name$$', $options['type'], $options['options']);
-            $builder->setAttribute('prototype', $prototype->getForm());
+            $builder->setAttribute('prototype', $prototype);
         }
 
         $listener = new ResizeFormListener(
@@ -55,7 +55,7 @@ class CollectionType extends AbstractType
         ;
 
         if ($form->hasAttribute('prototype')) {
-            $view->set('prototype', $form->getAttribute('prototype')->createView());
+            $view->set('prototype', $form->getAttribute('prototype'));
         }
     }
 

+ 11 - 0
src/Symfony/Component/Form/Form.php

@@ -921,6 +921,17 @@ class Form implements \IteratorAggregate, FormInterface
             $childViews[$key] = $child->createView($view);
         }
 
+        if (null !== $prototype = $view->get('prototype')) {
+            $protoView = $prototype->getForm()->createView($view);
+            $protoTypes = $protoView->get('types');
+            array_unshift($protoTypes, 'prototype');
+            $protoView
+                ->set('types', $protoTypes)
+                ->set('proto_id', $protoView->getParent()->get('id').'_prototype');
+            ;
+            $childViews[$prototype->getName()] = $protoView;
+        }
+
         $view->setChildren($childViews);
 
         foreach ($types as $type) {

+ 14 - 1
tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php

@@ -121,11 +121,24 @@ class CollectionFormTest extends TypeTestCase
     public function testAllowAddButNoPrototype()
     {
         $form = $this->factory->create('collection', null, array(
-            'type' => 'field',
+            'type'      => 'field',
             'allow_add' => true,
             'prototype' => false,
         ));
 
         $this->assertFalse($form->has('$$name$$'));
     }
+
+    public function testPrototypeMultipartPropagation()
+    {
+        $form = $this->factory
+            ->create('collection', null, array(
+                'type'      => 'file',
+                'allow_add' => true,
+                'prototype' => true,
+            ))
+        ;
+
+        $this->assertTrue($form->createView()->get('multipart'));
+    }
 }