فهرست منبع

Merge remote branch 'jaugustin/fix_form' into jaugustin_merge

Conflicts:
	src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php
	src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php
Bernhard Schussek 14 سال پیش
والد
کامیت
2711fbc418

+ 12 - 6
src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php

@@ -47,12 +47,18 @@ class ResizeFormListener implements EventSubscriberInterface
      */
     private $allowDelete;
 
-    public function __construct(FormFactoryInterface $factory, $type, $allowAdd = false, $allowDelete = false)
+    /**
+     * @var array
+     */
+    private $typeOptions;
+
+    public function __construct(FormFactoryInterface $factory, $type, $allowAdd = false, $allowDelete = false, array $typeOptions = array())
     {
         $this->factory = $factory;
         $this->type = $type;
         $this->allowAdd = $allowAdd;
         $this->allowDelete = $allowDelete;
+        $this->typeOptions = $typeOptions;
     }
 
     public static function getSubscribedEvents()
@@ -86,9 +92,9 @@ class ResizeFormListener implements EventSubscriberInterface
 
         // Then add all rows again in the correct order
         foreach ($data as $name => $value) {
-            $form->add($this->factory->createNamed($this->type, $name, null, array(
+            $form->add($this->factory->createNamed($this->type, $name, null, array_merge(array(
                 'property_path' => '['.$name.']',
-            )));
+            ), $this->typeOptions)));
         }
     }
 
@@ -118,9 +124,9 @@ class ResizeFormListener implements EventSubscriberInterface
         if ($this->allowAdd) {
             foreach ($data as $name => $value) {
                 if (!$form->has($name)) {
-                    $form->add($this->factory->createNamed($this->type, $name, null, array(
+                    $form->add($this->factory->createNamed($this->type, $name, null, array_merge(array(
                         'property_path' => '['.$name.']',
-                    )));
+                    ), $this->typeOptions)));
                 }
             }
         }
@@ -149,4 +155,4 @@ class ResizeFormListener implements EventSubscriberInterface
 
         $event->setData($data);
     }
-}
+}

+ 6 - 4
src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php

@@ -22,17 +22,18 @@ class CollectionType extends AbstractType
     public function buildForm(FormBuilder $builder, array $options)
     {
         if ($options['allow_add'] && $options['prototype']) {
-            $builder->add('$$name$$', $options['type'], array(
+            $builder->add('$$name$$', $options['type'], array_merge(array(
                 'property_path' => false,
-                'required'      => false,
-            ));
+                'required' => false,
+            ), $options['type_options']));
         }
 
         $listener = new ResizeFormListener(
             $builder->getFormFactory(),
             $options['type'],
             $options['allow_add'],
-            $options['allow_delete']
+            $options['allow_delete'],
+            $options['type_options']
         );
 
         $builder
@@ -57,6 +58,7 @@ class CollectionType extends AbstractType
             'allow_delete'  => false,
             'prototype'     => true,
             'type'          => 'text',
+            'type_options'  => array(),
         );
     }
 

+ 87 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php

@@ -143,4 +143,91 @@ class CollectionFormTest extends TypeTestCase
 
         $this->assertFalse($form->has('$$name$$'));
     }
+
+    public function testSetTypeOptions()
+    {
+        $form = $this->factory->create('collection', null, array(
+            'type' => 'field',
+            'type_options' => array(
+                'required' => false,
+                'max_length' => 20
+            ),
+        ));
+        $form->setData(array('foo@foo.com', 'foo@bar.com'));
+
+        $this->assertFalse($form[0]->isRequired());
+        $this->assertFalse($form[1]->isRequired());
+        $this->assertEquals(20, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(20, $form[1]->getAttribute('max_length'));
+
+        $form->bind(array('foo@bar.com', 'bar@foo.com'));
+
+        $this->assertFalse($form[0]->isRequired());
+        $this->assertFalse($form[1]->isRequired());
+        $this->assertEquals(20, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(20, $form[1]->getAttribute('max_length'));
+
+        //Test with prototype and extra field
+        $form = $this->factory->create('collection', null, array(
+            'allow_add' => true,
+            'prototype' => true,
+            'type' => 'field',
+            'type_options' => array(
+                'required' => false,
+                'max_length' => 20
+            ),
+        ));
+
+        $form->setData(array('foo@foo.com'));
+
+        $this->assertFalse($form[0]->isRequired());
+        $this->assertEquals(20, $form[0]->getAttribute('max_length'));
+
+        $form->bind(array('foo@bar.com', 'bar@foo.com'));
+
+        $this->assertFalse($form[0]->isRequired());
+        $this->assertFalse($form[1]->isRequired());
+        $this->assertEquals(20, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(20, $form[1]->getAttribute('max_length'));
+
+    }
+
+    public function testSetTypeOptionsWithoutOptions()
+    {
+        $form = $this->factory->create('collection', null, array(
+            'type' => 'field',
+        ));
+        $form->setData(array('foo@foo.com', 'foo@bar.com'));
+
+        $this->assertTrue($form[0]->isRequired());
+        $this->assertTrue($form[1]->isRequired());
+        $this->assertNull($form[0]->getAttribute('max_length'));
+        $this->assertNull($form[1]->getAttribute('max_length'));
+
+        $form->bind(array('foo@bar.com', 'bar@foo.com'));
+
+        $this->assertTrue($form[0]->isRequired());
+        $this->assertTrue($form[1]->isRequired());
+        $this->assertNull($form[0]->getAttribute('max_length'));
+        $this->assertNull($form[1]->getAttribute('max_length'));
+
+        //Test with prototype and extra field
+        $form = $this->factory->create('collection', null, array(
+            'allow_add' => true,
+            'prototype' => true,
+            'type' => 'field',
+        ));
+
+        $form->setData(array('foo@foo.com'));
+
+        $this->assertTrue($form[0]->isRequired());
+        $this->assertNull($form[0]->getAttribute('max_length'));
+
+        $form->bind(array('foo@bar.com', 'bar@foo.com'));
+
+        $this->assertTrue($form[0]->isRequired());
+        $this->assertTrue($form[1]->isRequired());
+        $this->assertNull($form[0]->getAttribute('max_length'));
+        $this->assertNull($form[1]->getAttribute('max_length'));
+    }
 }