Browse Source

[Form] Removed FormBuilder::setDataClass(). Use the FieldType option "data_class" instead.

The problem was that "data_class" was used in two places: FormBuilder::build() and PropertyPathMapper.

PropertyPathMapper was already constructed during FormType::buildForm(), so any data class changes made to the FormBuilder wouldn't affect the data class of the PropertyPathMapper anymore and so lead to an inconsistent state.
Bernhard Schussek 14 years ago
parent
commit
6588a47ba7

+ 2 - 13
src/Symfony/Component/Form/FormBuilder.php

@@ -54,9 +54,10 @@ class FormBuilder
 
     private $errorBubbling = false;
 
-    public function __construct(EventDispatcherInterface $dispatcher)
+    public function __construct(EventDispatcherInterface $dispatcher, $dataClass = null)
     {
         $this->dispatcher = $dispatcher;
+        $this->dataClass = $dataClass;
     }
 
     public function setFormFactory(FormFactoryInterface $factory)
@@ -410,18 +411,6 @@ class FormBuilder
         return $children;
     }
 
-    public function setDataClass($class)
-    {
-        $this->dataClass = $class;
-
-        return $this;
-    }
-
-    public function getDataClass()
-    {
-        return $this->dataClass;
-    }
-
     public function getForm()
     {
         $instance = new Form(

+ 2 - 1
src/Symfony/Component/Form/Type/FieldType.php

@@ -97,6 +97,7 @@ class FieldType extends AbstractType
         return array(
             'template' => 'text', // TODO remove me
             'data' => null,
+            'data_class' => null,
             'trim' => true,
             'required' => true,
             'read_only' => false,
@@ -111,7 +112,7 @@ class FieldType extends AbstractType
 
     public function createBuilder(array $options)
     {
-        return new FormBuilder(new EventDispatcher());
+        return new FormBuilder(new EventDispatcher(), $options['data_class']);
     }
 
     public function getParent(array $options)

+ 0 - 2
src/Symfony/Component/Form/Type/FormType.php

@@ -23,7 +23,6 @@ class FormType extends AbstractType
     public function buildForm(FormBuilder $builder, array $options)
     {
         $builder->setAttribute('virtual', $options['virtual'])
-            ->setDataClass($options['data_class'])
             ->setDataMapper(new PropertyPathMapper(
                 $options['data_class'],
                 $options['data_constructor']
@@ -58,7 +57,6 @@ class FormType extends AbstractType
     {
         return array(
             'template' => 'form',
-            'data_class' => null,
             'data_constructor' => null,
             'csrf_protection' => true,
             'csrf_field_name' => '_token',

+ 7 - 5
tests/Symfony/Tests/Component/Form/FormBuilderTest.php

@@ -19,12 +19,14 @@ use Symfony\Component\Form\Type\Guesser\TypeGuess;
 
 class FormBuilderTest extends \PHPUnit_Framework_TestCase
 {
+    private $dispatcher;
+
     private $builder;
 
     public function setUp()
     {
-        $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
-        $this->builder = new FormBuilder($dispatcher);
+        $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+        $this->builder = new FormBuilder($this->dispatcher);
     }
 
     public function testAddNameNoString()
@@ -41,7 +43,7 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
 
     public function testAddWithGuessFluent()
     {
-        $this->builder->setDataClass('stdClass');
+        $this->builder = new FormBuilder($this->dispatcher, 'stdClass');
         $builder = $this->builder->add('foo');
         $this->assertSame($builder, $this->builder);
     }
@@ -120,9 +122,9 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
                 ->method('createBuilderForProperty')
                 ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
                 ->will($this->returnValue($this->getFormBuilder()));
-        $this->builder->setFormFactory($factory);
 
-        $this->builder->setDataClass('stdClass');
+        $this->builder = new FormBuilder($this->dispatcher, 'stdClass');
+        $this->builder->setFormFactory($factory);
         $this->builder->add($expectedName, null, $expectedOptions);
         $builder = $this->builder->get($expectedName);