浏览代码

[Form] Fixed prototype option in CollectionField

Bernhard Schussek 14 年之前
父节点
当前提交
eb918ac86d
共有 1 个文件被更改,包括 22 次插入3 次删除
  1. 22 3
      src/Symfony/Component/Form/CollectionField.php

+ 22 - 3
src/Symfony/Component/Form/CollectionField.php

@@ -32,6 +32,26 @@ class CollectionField extends Form
      */
     protected $removedFields = array();
 
+    /**
+     * The prototype field for the collection rows
+     * @var FieldInterface
+     */
+    protected $prototype;
+
+    public function __construct($key, array $options = array())
+    {
+        // This doesn't work with addOption(), because the value of this option
+        // needs to be accessed before Configurable::__construct() is reached
+        // Setting all options in the constructor of the root field
+        // is conceptually flawed
+        if (isset($options['prototype'])) {
+            $this->prototype = $options['prototype'];
+            unset($options['prototype']);
+        }
+
+        parent::__construct($key, $options);
+    }
+
     /**
      * Available options:
      *
@@ -43,7 +63,6 @@ class CollectionField extends Form
      */
     protected function configure()
     {
-        $this->addOption('prototype');
         $this->addOption('modifiable', false);
 
         if ($this->getOption('modifiable')) {
@@ -114,8 +133,8 @@ class CollectionField extends Form
             $propertyPath = '['.$propertyPath.']';
         }
 
-        if ($this->getOption('prototype')) {
-            $field = clone $this->getOption('prototype');
+        if ($this->prototype) {
+            $field = clone $this->prototype;
             $field->setKey($key);
             $field->setPropertyPath($propertyPath);
         } else {