Jelajahi Sumber

[Form] Forms now remember the types used during their construction

Bernhard Schussek 14 tahun lalu
induk
melakukan
15d5259eb8

+ 9 - 1
src/Symfony/Component/Form/Form.php

@@ -106,8 +106,10 @@ class Form implements \IteratorAggregate, FormInterface
     private $readOnly = false;
     private $dispatcher;
     private $attributes;
+    private $types;
 
-    public function __construct($name, EventDispatcherInterface $dispatcher,
+    public function __construct($name, array $types,
+        EventDispatcherInterface $dispatcher,
         FormRendererInterface $renderer = null, DataTransformerInterface $clientTransformer = null,
         DataTransformerInterface $normTransformer = null,
         DataMapperInterface $dataMapper = null, array $validators = array(),
@@ -121,6 +123,7 @@ class Form implements \IteratorAggregate, FormInterface
         }
 
         $this->name = (string)$name;
+        $this->types = $types;
         $this->dispatcher = $dispatcher;
         $this->renderer = $renderer;
         $this->clientTransformer = $clientTransformer;
@@ -154,6 +157,11 @@ class Form implements \IteratorAggregate, FormInterface
         return $this->name;
     }
 
+    public function getTypes()
+    {
+        return $this->types;
+    }
+
     /**
      * {@inheritDoc}
      */

+ 15 - 0
src/Symfony/Component/Form/FormBuilder.php

@@ -48,6 +48,8 @@ class FormBuilder
 
     private $attributes = array();
 
+    private $types = array();
+
     private $parent;
 
     private $dataClass;
@@ -308,6 +310,18 @@ class FormBuilder
         return $this->dataMapper;
     }
 
+    public function setTypes(array $types)
+    {
+        $this->types = $types;
+
+        return $this;
+    }
+
+    public function getTypes()
+    {
+        return $this->types;
+    }
+
     /**
      * Adds a new field to this group. A field must have a unique name within
      * the group. Otherwise the existing field is overwritten.
@@ -462,6 +476,7 @@ class FormBuilder
     {
         $instance = new Form(
             $this->getName(),
+            $this->getTypes(),
             $this->buildDispatcher(),
             $this->buildRenderer(),
             $this->getClientTransformer(),

+ 4 - 3
src/Symfony/Component/Form/FormFactory.php

@@ -37,7 +37,7 @@ class FormFactory implements FormFactoryInterface
         // TODO $type can be FQN of a type class
 
         $builder = null;
-        $hierarchy = array();
+        $types = array();
 
         // TESTME
         if (null === $name && preg_match('/\w+$/', $type, $matches)) {
@@ -47,7 +47,7 @@ class FormFactory implements FormFactoryInterface
         while (null !== $type) {
             // TODO check if type exists
             $type = $this->typeLoader->getType($type);
-            array_unshift($hierarchy, $type);
+            array_unshift($types, $type);
             $options = array_merge($type->getDefaultOptions($options), $options);
             $builder = $builder ?: $type->createBuilder($options);
             $type = $type->getParent($options);
@@ -56,9 +56,10 @@ class FormFactory implements FormFactoryInterface
         // TODO check if instance exists
 
         $builder->setName($name);
+        $builder->setTypes($types);
         $builder->setFormFactory($this);
 
-        foreach ($hierarchy as $type) {
+        foreach ($types as $type) {
             $type->configure($builder, $options);
         }