Jelajahi Sumber

[Form] add type_options for CollectionType to be abble to set options to type

Jeremie Augustin 14 tahun lalu
induk
melakukan
b5277752b7

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

@@ -40,12 +40,18 @@ class ResizeFormListener implements EventSubscriberInterface
      */
     private $allowAdd;
 
-    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()
@@ -79,9 +85,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)));
         }
     }
 
@@ -111,9 +117,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)));
                 }
             }
         }
@@ -142,4 +148,4 @@ class ResizeFormListener implements EventSubscriberInterface
 
         $event->setData($data);
     }
-}
+}

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

@@ -22,14 +22,14 @@ 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,
-            ));
+            ), $options['type_options']));
         }
 
         $listener = new ResizeFormListener($builder->getFormFactory(),
-                $options['type'], $options['allow_add'], $options['allow_delete']);
+                $options['type'], $options['allow_add'], $options['allow_delete'], $options['type_options']);
 
         $builder->addEventSubscriber($listener)
             ->setAttribute('allow_add', $options['allow_add'])
@@ -49,6 +49,7 @@ class CollectionType extends AbstractType
             'allow_delete' => false,
             'prototype'  => true,
             'type' => 'text',
+            'type_options'  => array(),
         );
     }