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

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

@@ -97,6 +97,7 @@ class FieldType extends AbstractType
         return array(
         return array(
             'template' => 'text', // TODO remove me
             'template' => 'text', // TODO remove me
             'data' => null,
             'data' => null,
+            'data_class' => null,
             'trim' => true,
             'trim' => true,
             'required' => true,
             'required' => true,
             'read_only' => false,
             'read_only' => false,
@@ -111,7 +112,7 @@ class FieldType extends AbstractType
 
 
     public function createBuilder(array $options)
     public function createBuilder(array $options)
     {
     {
-        return new FormBuilder(new EventDispatcher());
+        return new FormBuilder(new EventDispatcher(), $options['data_class']);
     }
     }
 
 
     public function getParent(array $options)
     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)
     public function buildForm(FormBuilder $builder, array $options)
     {
     {
         $builder->setAttribute('virtual', $options['virtual'])
         $builder->setAttribute('virtual', $options['virtual'])
-            ->setDataClass($options['data_class'])
             ->setDataMapper(new PropertyPathMapper(
             ->setDataMapper(new PropertyPathMapper(
                 $options['data_class'],
                 $options['data_class'],
                 $options['data_constructor']
                 $options['data_constructor']
@@ -58,7 +57,6 @@ class FormType extends AbstractType
     {
     {
         return array(
         return array(
             'template' => 'form',
             'template' => 'form',
-            'data_class' => null,
             'data_constructor' => null,
             'data_constructor' => null,
             'csrf_protection' => true,
             'csrf_protection' => true,
             'csrf_field_name' => '_token',
             '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
 class FormBuilderTest extends \PHPUnit_Framework_TestCase
 {
 {
+    private $dispatcher;
+
     private $builder;
     private $builder;
 
 
     public function setUp()
     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()
     public function testAddNameNoString()
@@ -41,7 +43,7 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
 
 
     public function testAddWithGuessFluent()
     public function testAddWithGuessFluent()
     {
     {
-        $this->builder->setDataClass('stdClass');
+        $this->builder = new FormBuilder($this->dispatcher, 'stdClass');
         $builder = $this->builder->add('foo');
         $builder = $this->builder->add('foo');
         $this->assertSame($builder, $this->builder);
         $this->assertSame($builder, $this->builder);
     }
     }
@@ -120,9 +122,9 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
                 ->method('createBuilderForProperty')
                 ->method('createBuilderForProperty')
                 ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
                 ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
                 ->will($this->returnValue($this->getFormBuilder()));
                 ->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);
         $this->builder->add($expectedName, null, $expectedOptions);
         $builder = $this->builder->get($expectedName);
         $builder = $this->builder->get($expectedName);