Przeglądaj źródła

[Form] Made name immutable in FormBuilder to avoid synchronization problems with name and property path

Bernhard Schussek 14 lat temu
rodzic
commit
1ef18360fa

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

@@ -56,8 +56,9 @@ class FormBuilder
 
     private $emptyData = null;
 
-    public function __construct(EventDispatcherInterface $dispatcher, $dataClass = null)
+    public function __construct($name, EventDispatcherInterface $dispatcher, $dataClass = null)
     {
+        $this->name = $name;
         $this->dispatcher = $dispatcher;
         $this->dataClass = $dataClass;
     }
@@ -74,13 +75,6 @@ class FormBuilder
         return $this->factory;
     }
 
-    public function setName($name)
-    {
-        $this->name = $name;
-
-        return $this;
-    }
-
     public function getName()
     {
         return $this->name;

+ 1 - 2
src/Symfony/Component/Form/FormFactory.php

@@ -77,12 +77,11 @@ class FormFactory implements FormFactoryInterface
         }
 
         for ($i = 0, $l = count($types); $i < $l && !$builder; ++$i) {
-            $builder = $types[$i]->createBuilder($options);
+            $builder = $types[$i]->createBuilder($name, $options);
         }
 
         // TODO check if instance exists
 
-        $builder->setName($name);
         $builder->setTypes($types);
         $builder->setFormFactory($this);
 

+ 1 - 1
src/Symfony/Component/Form/Type/AbstractType.php

@@ -30,7 +30,7 @@ abstract class AbstractType implements FormTypeInterface
     {
     }
 
-    public function createBuilder(array $options)
+    public function createBuilder($name, array $options)
     {
         return null;
     }

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

@@ -118,9 +118,9 @@ class FieldType extends AbstractType
         return $defaultOptions;
     }
 
-    public function createBuilder(array $options)
+    public function createBuilder($name, array $options)
     {
-        return new FormBuilder(new EventDispatcher(), $options['data_class']);
+        return new FormBuilder($name, new EventDispatcher(), $options['data_class']);
     }
 
     public function getParent(array $options)

+ 1 - 1
src/Symfony/Component/Form/Type/FormTypeInterface.php

@@ -23,7 +23,7 @@ interface FormTypeInterface
 
     function buildRendererBottomUp(ThemeRendererInterface $renderer, FormInterface $form);
 
-    function createBuilder(array $options);
+    function createBuilder($name, array $options);
 
     function getDefaultOptions(array $options);
 

+ 17 - 6
tests/Symfony/Tests/Component/Form/FormBuilderTest.php

@@ -26,7 +26,18 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
     public function setUp()
     {
         $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
-        $this->builder = new FormBuilder($this->dispatcher);
+        $this->builder = new FormBuilder('name', $this->dispatcher);
+    }
+
+    /**
+     * Changing the name is not allowed, otherwise the name and property path
+     * are not synchronized anymore
+     *
+     * @see FieldType::buildForm
+     */
+    public function testNoSetName()
+    {
+        $this->assertFalse(method_exists($this->builder, 'setName'));
     }
 
     public function testAddNameNoString()
@@ -38,19 +49,19 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
     public function testAddTypeNoString()
     {
         $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
-        $this->builder->add("foo", 1234);
+        $this->builder->add('foo', 1234);
     }
 
     public function testAddWithGuessFluent()
     {
-        $this->builder = new FormBuilder($this->dispatcher, 'stdClass');
+        $this->builder = new FormBuilder('name', $this->dispatcher, 'stdClass');
         $builder = $this->builder->add('foo');
         $this->assertSame($builder, $this->builder);
     }
 
     public function testAddIsFluent()
     {
-        $builder = $this->builder->add("foo", "text", array("bar" => "baz"));
+        $builder = $this->builder->add('foo', 'text', array('bar' => 'baz'));
         $this->assertSame($builder, $this->builder);
     }
 
@@ -84,7 +95,7 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
     public function testBuildNoTypeNoDataClass()
     {
         $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The data class must be set to automatically create children');
-        $this->builder->build("foo");
+        $this->builder->build('foo');
     }
 
     public function testGetUnknown()
@@ -123,7 +134,7 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
                 ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
                 ->will($this->returnValue($this->getFormBuilder()));
 
-        $this->builder = new FormBuilder($this->dispatcher, 'stdClass');
+        $this->builder = new FormBuilder('name', $this->dispatcher, 'stdClass');
         $this->builder->setFormFactory($factory);
         $this->builder->add($expectedName, null, $expectedOptions);
         $builder = $this->builder->get($expectedName);

+ 1 - 1
tests/Symfony/Tests/Component/Form/FormTest.php

@@ -26,7 +26,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
     protected function setUp()
     {
         $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
-        $this->builder = new FormBuilder($this->dispatcher);
+        $this->builder = new FormBuilder('name', $this->dispatcher);
         $this->form = $this->builder->getForm();
     }
 

+ 1 - 1
tests/Symfony/Tests/Component/Form/Type/TestCase.php

@@ -61,7 +61,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
 
         $this->factory = new FormFactory($this->typeLoader, $this->rendererFactoryLoader);
 
-        $this->builder = new FormBuilder($this->dispatcher);
+        $this->builder = new FormBuilder('name', $this->dispatcher);
     }
 
     protected function getTypeLoaders()

+ 1 - 2
tests/Symfony/Tests/Component/Form/Validator/DelegatingValidatorTest.php

@@ -71,8 +71,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
 
     protected function getBuilder($name, $propertyPath = null)
     {
-        $builder = new FormBuilder($this->dispatcher);
-        $builder->setName($name);
+        $builder = new FormBuilder($name, $this->dispatcher);
         $builder->setAttribute('property_path', new PropertyPath($propertyPath ?: $name));
         $builder->setAttribute('error_mapping', array());