Kaynağa Gözat

Added support for additional attributes in Form types that list field as their parent.

Miha Vrhovnik 14 yıl önce
ebeveyn
işleme
c7074672ba

+ 8 - 2
src/Symfony/Component/Form/Extension/Core/Type/FieldType.php

@@ -20,6 +20,7 @@ use Symfony\Component\Form\FormView;
 use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
 use Symfony\Component\Form\Extension\Core\Validator\DefaultValidator;
 use Symfony\Component\EventDispatcher\EventDispatcher;
+use Symfony\Component\Form\Exception\FormException;
 
 class FieldType extends AbstractType
 {
@@ -34,7 +35,10 @@ class FieldType extends AbstractType
         } else {
             $options['property_path'] = new PropertyPath($options['property_path']);
         }
-
+        if (!is_array($options['attributes'])) {
+            throw new FormException('attributes option should be array');
+        }
+        
         $builder
             ->setRequired($options['required'])
             ->setReadOnly($options['read_only'])
@@ -46,6 +50,7 @@ class FieldType extends AbstractType
             ->setAttribute('max_length', $options['max_length'])
             ->setAttribute('pattern', $options['pattern'])
             ->setAttribute('label', $options['label'] ?: $this->humanize($builder->getName()))
+            ->setAttribute('attributes', $options['attributes'] ?: array())
             ->setData($options['data'])
             ->addValidator(new DefaultValidator())
         ;
@@ -88,7 +93,7 @@ class FieldType extends AbstractType
             ->set('size', null)
             ->set('label', $form->getAttribute('label'))
             ->set('multipart', false)
-            ->set('attr', array())
+            ->set('attr', $form->getAttribute('attributes'))
             ->set('types', $types)
         ;
     }
@@ -108,6 +113,7 @@ class FieldType extends AbstractType
             'error_bubbling'    => false,
             'error_mapping'     => array(),
             'label'             => null,
+            'attributes'        => array(),
         );
 
         $class = isset($options['data_class']) ? $options['data_class'] : null;

+ 16 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php

@@ -195,4 +195,20 @@ class FieldTypeTest extends TypeTestCase
         $this->assertSame($author, $form->getData());
         $this->assertEquals('Bernhard', $author->firstName);
     }
+    
+    public function testGetAttributesIsEmpty()
+    {
+        $form = $this->factory->create('field', null, array('attributes' => array()));
+
+        $this->assertEquals(0, count($form->getAttribute('attributes')));
+    }
+
+    /**
+     * @expectedException Symfony\Component\Form\Exception\FormException
+     */
+    public function testAttributesException()
+    {
+        $form = $this->factory->create('field', null, array('attributes' => ''));
+    }
+
 }