Jelajahi Sumber

[Form] Collection's prototype is not not a child anymore.

marc.weistroff 14 tahun lalu
induk
melakukan
257f86cb20

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

@@ -28,7 +28,6 @@ class CollectionType extends AbstractType
         if ($options['allow_add'] && $options['prototype']) {
             $prototype = $builder->create('$$name$$', $options['type'], $options['options']);
             $builder->setAttribute('prototype', $prototype->getForm());
-            $builder->add($prototype);
         }
 
         $dataClass = isset($options['options']['data_class']) ? $options['options']['data_class'] : null;
@@ -68,6 +67,16 @@ class CollectionType extends AbstractType
         }
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function buildViewBottomUp(FormView $view, FormInterface $form)
+    {
+        if ($form->hasAttribute('prototype') && $view->get('prototype')->get('multipart')) {
+            $view->set('multipart', true);
+        }
+    }
+
     /**
      * {@inheritdoc}
      */

+ 25 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php

@@ -178,4 +178,29 @@ class CollectionFormTest extends TypeTestCase
         $bound = $form->getData();
         $this->assertInstanceOf('Symfony\Tests\Component\Form\Fixtures\Author', $bound[0]);
     }
+
+    public function testGetDataDoesNotContainsProtypeNameBeforeDataAreSet()
+    {
+        $form = $this->factory->create('collection', array(), array(
+            'type'      => 'file',
+            'prototype' => true,
+            'allow_add' => true,
+        ));
+
+        $data = $form->getData();
+        $this->assertFalse(isset($data['$$name$$']));
+    }
+
+    public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet()
+    {
+        $form = $this->factory->create('collection', array(), array(
+            'type'      => 'file',
+            'allow_add' => true,
+            'prototype' => true,
+        ));
+
+        $form->setData(array('foobar.png'));
+        $data = $form->getData();
+        $this->assertFalse(isset($data['$$name$$']));
+    }
 }