Forráskód Böngészése

[Form] CollectionType now checks for data_class parameter instead of only class.

marc.weistroff 14 éve
szülő
commit
2e024f87a3

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

@@ -30,8 +30,10 @@ class CollectionType extends AbstractType
             $builder->setAttribute('prototype', $prototype);
         }
 
-        if ($options['class']) {
-            $listener = new ObjectFactoryListener($options['class']);
+        $dataClass = isset($options['options']['data_class']) ? $options['options']['data_class'] : null;
+        if ($dataClass || $options['class']) {
+            $class = $dataClass ? $dataClass : $options['class'];
+            $listener = new ObjectFactoryListener($class);
             $builder->addEventSubscriber($listener);
         }
 

+ 17 - 8
tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php

@@ -153,14 +153,7 @@ class CollectionFormTest extends TypeTestCase
             ))
         ;
 
-        $data = array(
-            array(
-                'last_name' => 'Foo'
-            ),
-            array(
-                'last_name' => 'Bar'
-            ),
-        );
+        $data = array(array('last_name' => 'Foo'), array('last_name' => 'Bar'));
         $form->bind($data);
         $bound = $form->getData();
         $this->assertEquals(2, count($bound));
@@ -169,4 +162,20 @@ class CollectionFormTest extends TypeTestCase
         $this->assertEquals('Foo', $bound[0]->getLastName());
         $this->assertEquals('Bar', $bound[1]->getLastName());
     }
+
+    public function testObjectsAreCreatedWithDataClassOption()
+    {
+        $form = $this->factory
+            ->create('collection', null, array(
+                'type'      => new AuthorType(),
+                'allow_add' => true,
+                'options'   => array('data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author')
+            ))
+        ;
+
+        $data = array(array('last_name' => 'Foo'), array('last_name' => 'Bar'));
+        $form->bind($data);
+        $bound = $form->getData();
+        $this->assertInstanceOf('Symfony\Tests\Component\Form\Fixtures\Author', $bound[0]);
+    }
 }