فهرست منبع

[Form] Adapted constructor of CollectionField to match the constructors of the other fields. The field prototype is now optional.

Bernhard Schussek 14 سال پیش
والد
کامیت
c923af2879
2فایلهای تغییر یافته به همراه46 افزوده شده و 37 حذف شده
  1. 21 27
      src/Symfony/Component/Form/CollectionField.php
  2. 25 10
      tests/Symfony/Tests/Component/Form/CollectionFieldTest.php

+ 21 - 27
src/Symfony/Component/Form/CollectionField.php

@@ -26,30 +26,12 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
  */
 class CollectionField extends Form
 {
-    /**
-     * The prototype for the inner fields
-     * @var FieldInterface
-     */
-    protected $prototype;
-
     /**
      * Remembers which fields were removed upon submitting
      * @var array
      */
     protected $removedFields = array();
 
-    /**
-     * Repeats the given field multiple times based in the internal collection.
-     *
-     * @param FieldInterface $innerField
-     */
-    public function __construct(FieldInterface $innerField, array $options = array())
-    {
-        $this->prototype = $innerField;
-
-        parent::__construct($innerField->getKey(), $options);
-    }
-
     /**
      * Available options:
      *
@@ -61,6 +43,7 @@ class CollectionField extends Form
      */
     protected function configure()
     {
+        $this->addOption('prototype');
         $this->addOption('modifiable', false);
 
         if ($this->getOption('modifiable')) {
@@ -92,28 +75,28 @@ class CollectionField extends Form
         parent::setData($collection);
     }
 
-    public function submit($taintedData)
+    public function submit($data)
     {
         $this->removedFields = array();
 
-        if (null === $taintedData) {
-            $taintedData = array();
+        if (null === $data) {
+            $data = array();
         }
 
         foreach ($this as $name => $field) {
-            if (!isset($taintedData[$name]) && $this->getOption('modifiable') && '$$key$$' != $name) {
+            if (!isset($data[$name]) && $this->getOption('modifiable') && '$$key$$' != $name) {
                 $this->remove($name);
                 $this->removedFields[] = $name;
             }
         }
 
-        foreach ($taintedData as $name => $value) {
+        foreach ($data as $name => $value) {
             if (!isset($this[$name]) && $this->getOption('modifiable')) {
                 $this->add($this->newField($name, $name));
             }
         }
 
-        parent::submit($taintedData);
+        parent::submit($data);
     }
 
     protected function writeObject(&$objectOrArray)
@@ -127,9 +110,20 @@ class CollectionField extends Form
 
     protected function newField($key, $propertyPath)
     {
-        $field = clone $this->prototype;
-        $field->setKey($key);
-        $field->setPropertyPath(null === $propertyPath ? null : '['.$propertyPath.']');
+        if (null !== $propertyPath) {
+            $propertyPath = '['.$propertyPath.']';
+        }
+
+        if ($this->getOption('prototype')) {
+            $field = clone $this->getOption('prototype');
+            $field->setKey($key);
+            $field->setPropertyPath($propertyPath);
+        } else {
+            $field = new TextField($key, array(
+                'property_path' => $propertyPath,
+            ));
+        }
+
         return $field;
     }
 }

+ 25 - 10
tests/Symfony/Tests/Component/Form/CollectionFieldTest.php

@@ -21,13 +21,17 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase
 {
     public function testContainsNoFieldsByDefault()
     {
-        $field = new CollectionField(new TestField('emails'));
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
+        ));
         $this->assertEquals(0, count($field));
     }
 
     public function testSetDataAdjustsSize()
     {
-        $field = new CollectionField(new TestField('emails'));
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
+        ));
         $field->setData(array('foo@foo.com', 'foo@bar.com'));
 
         $this->assertTrue($field[0] instanceof TestField);
@@ -45,7 +49,8 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase
 
     public function testSetDataAdjustsSizeIfModifiable()
     {
-        $field = new CollectionField(new TestField('emails'), array(
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
             'modifiable' => true,
         ));
         $field->setData(array('foo@foo.com', 'foo@bar.com'));
@@ -64,14 +69,17 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase
 
     public function testThrowsExceptionIfObjectIsNotTraversable()
     {
-        $field = new CollectionField(new TestField('emails'));
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
+        ));
         $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
         $field->setData(new \stdClass());
     }
 
     public function testModifiableCollectionsContainExtraField()
     {
-        $field = new CollectionField(new TestField('emails'), array(
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
             'modifiable' => true,
         ));
         $field->setData(array('foo@bar.com'));
@@ -83,7 +91,9 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase
 
     public function testNotResizedIfSubmittedWithMissingData()
     {
-        $field = new CollectionField(new TestField('emails'));
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
+        ));
         $field->setData(array('foo@foo.com', 'bar@bar.com'));
         $field->submit(array('foo@bar.com'));
 
@@ -95,7 +105,8 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase
 
     public function testResizedIfSubmittedWithMissingDataAndModifiable()
     {
-        $field = new CollectionField(new TestField('emails'), array(
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
             'modifiable' => true,
         ));
         $field->setData(array('foo@foo.com', 'bar@bar.com'));
@@ -108,7 +119,9 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase
 
     public function testNotResizedIfSubmittedWithExtraData()
     {
-        $field = new CollectionField(new TestField('emails'));
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
+        ));
         $field->setData(array('foo@bar.com'));
         $field->submit(array('foo@foo.com', 'bar@bar.com'));
 
@@ -119,7 +132,8 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase
 
     public function testResizedUpIfSubmittedWithExtraDataAndModifiable()
     {
-        $field = new CollectionField(new TestField('emails'), array(
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
             'modifiable' => true,
         ));
         $field->setData(array('foo@bar.com'));
@@ -134,7 +148,8 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase
 
     public function testResizedDownIfSubmittedWithLessDataAndModifiable()
     {
-        $field = new CollectionField(new TestField('emails'), array(
+        $field = new CollectionField('emails', array(
+            'prototype' => new TestField(),
             'modifiable' => true,
         ));
         $field->setData(array('foo@bar.com', 'bar@bar.com'));