Browse Source

[Form] Added fluid interface for inline adding of sub-builders to a builder

Bernhard Schussek 14 years ago
parent
commit
fb2db58801

+ 19 - 0
src/Symfony/Component/Form/FieldBuilder.php

@@ -48,6 +48,8 @@ class FieldBuilder
 
     private $attributes = array();
 
+    private $parent;
+
     public function __construct(ThemeInterface $theme,
             EventDispatcherInterface $dispatcher)
     {
@@ -79,6 +81,23 @@ class FieldBuilder
         return $this->name;
     }
 
+    public function setParent(FieldBuilder $builder)
+    {
+        $this->parent = $builder;
+
+        return $this;
+    }
+
+    public function getParent()
+    {
+        return $this->parent;
+    }
+
+    public function end()
+    {
+        return $this->parent;
+    }
+
     public function setData($data)
     {
         $this->data = $data;

+ 39 - 35
src/Symfony/Component/Form/FormBuilder.php

@@ -81,29 +81,43 @@ class FormBuilder extends FieldBuilder
             throw new UnexpectedTypeException($name, 'string');
         }
 
+        if (null !== $type && !is_string($type)) {
+            throw new UnexpectedTypeException($type, 'string');
+        }
+
+        $this->fields[$name] = array(
+            'type' => $type,
+            'options' => $options,
+        );
+
+        return $this;
+    }
+
+    public function build($name, $type = null, array $options = array())
+    {
         if (null !== $type) {
-            if (!is_string($type)) {
-                throw new UnexpectedTypeException($type, 'string');
+            $builder = $this->getFormFactory()->createBuilder(
+                $type,
+                $name,
+                $options
+            );
+        } else {
+            if (!$this->dataClass) {
+                throw new FormException('The data class must be set to automatically create fields');
             }
 
-            $this->fields[$name] = array(
-                'type' => $type,
-                'options' => $options,
+            $builder = $this->getFormFactory()->createBuilderForProperty(
+                $this->dataClass,
+                $name,
+                $options
             );
-
-            return $this;
         }
 
-        if (!$this->dataClass) {
-            throw new FormException('The data class must be set to automatically create fields');
-        }
+        $this->fields[$name] = $builder;
 
-        $this->fields[$name] = array(
-            'class' => $this->dataClass,
-            'options' => $options,
-        );
+        $builder->setParent($this);
 
-        return $this;
+        return $builder;
     }
 
     public function get($name)
@@ -112,27 +126,13 @@ class FormBuilder extends FieldBuilder
             throw new FormException(sprintf('The field "%s" does not exist', $name));
         }
 
-        if ($this->fields[$name] instanceof FieldBuilder) {
-            return $this->fields[$name];
-        }
+        $field = $this->fields[$name];
 
-        if (isset($this->fields[$name]['type'])) {
-            $this->fields[$name] = $this->getFormFactory()->createBuilder(
-                $this->fields[$name]['type'],
-                $name,
-                $this->fields[$name]['options']
-            );
-
-            return $this->fields[$name];
+        if ($field instanceof FieldBuilder) {
+            return $field;
         }
 
-        $this->fields[$name] = $this->getFormFactory()->createBuilderForProperty(
-            $this->fields[$name]['class'],
-            $name,
-            $this->fields[$name]['options']
-        );
-
-        return $this->fields[$name];
+        return $this->build($name, $field['type'], $field['options']);
     }
 
     /**
@@ -162,8 +162,12 @@ class FormBuilder extends FieldBuilder
     {
         $fields = array();
 
-        foreach ($this->fields as $name => $field) {
-            $fields[$name] = $this->get($name)->getInstance();
+        foreach ($this->fields as $name => $builder) {
+            if (!$builder instanceof FieldBuilder) {
+                $builder = $this->build($name, $builder['type'], $builder['options']);
+            }
+
+            $fields[$name] = $builder->getInstance();
         }
 
         return $fields;