Browse Source

[Form] Made InputField instantiable so that simple input fields can be created on the fly

Bernhard Schussek 14 năm trước cách đây
mục cha
commit
96a0bff915

+ 4 - 4
src/Symfony/Component/Form/CheckboxField.php

@@ -21,10 +21,10 @@ class CheckboxField extends ToggleField
     /**
      * {@inheritDoc}
      */
-    public function getAttributes()
+    public function __construct($key, array $options = array())
     {
-        return array_merge(parent::getAttributes(), array(
-            'type' => 'checkbox',
-        ));
+        $options['type'] = 'checkbox';
+
+        parent::__construct($key, $options);
     }
 }

+ 4 - 4
src/Symfony/Component/Form/HiddenField.php

@@ -21,11 +21,11 @@ class HiddenField extends InputField
     /**
      * {@inheritDoc}
      */
-    public function getAttributes()
+    public function __construct($key, array $options = array())
     {
-        return array_merge(parent::getAttributes(), array(
-            'type' => 'hidden',
-        ));
+        $options['type'] = 'hidden';
+
+        parent::__construct($key, $options);
     }
 
     /**

+ 12 - 1
src/Symfony/Component/Form/InputField.php

@@ -16,8 +16,18 @@ namespace Symfony\Component\Form;
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  */
-abstract class InputField extends Field
+class InputField extends Field
 {
+    /**
+     * {@inheritDoc}
+     */
+    protected function configure()
+    {
+        parent::configure();
+
+        $this->addRequiredOption('type');
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -28,6 +38,7 @@ abstract class InputField extends Field
             'name'     => $this->getName(),
             'value'    => $this->getDisplayedData(),
             'disabled' => $this->isDisabled(),
+            'type'     => $this->getOption('type'),
         ));
     }
 }

+ 2 - 2
src/Symfony/Component/Form/IntegerField.php

@@ -33,12 +33,12 @@ class IntegerField extends NumberField
      */
     protected function configure()
     {
+        parent::configure();
+
         $this->addOption('precision', 0);
 
         // Integer cast rounds towards 0, so do the same when displaying fractions
         $this->addOption('rounding-mode', NumberToLocalizedStringTransformer::ROUND_DOWN);
-
-        parent::configure();
     }
 
     /**

+ 12 - 10
src/Symfony/Component/Form/NumberField.php

@@ -20,11 +20,23 @@ use Symfony\Component\Form\ValueTransformer\NumberToLocalizedStringTransformer;
  */
 class NumberField extends InputField
 {
+    /**
+     * {@inheritDoc}
+     */
+    public function __construct($key, array $options = array())
+    {
+        $options['type'] = 'text';
+
+        parent::__construct($key, $options);
+    }
+
     /**
      * {@inheritDoc}
      */
     protected function configure()
     {
+        parent::configure();
+
         // default precision is locale specific (usually around 3)
         $this->addOption('precision');
         $this->addOption('grouping', false);
@@ -36,14 +48,4 @@ class NumberField extends InputField
             'rounding-mode' => $this->getOption('rounding-mode'),
         )));
     }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getAttributes(array $attributes = array())
-    {
-        return array_merge(parent::getAttributes(), array(
-            'type'  => 'text',
-        ));
-    }
 }

+ 1 - 0
src/Symfony/Component/Form/PasswordField.php

@@ -34,6 +34,7 @@ class PasswordField extends TextField
     public function getAttributes()
     {
         return array_merge(parent::getAttributes(), array(
+            // override getDisplayedData() instead?
             'value'       => $this->getOption('always_empty') && !$this->isBound() ? '' : $this->getDisplayedData(),
             'type'        => 'password',
         ));

+ 11 - 1
src/Symfony/Component/Form/RadioField.php

@@ -18,13 +18,23 @@ namespace Symfony\Component\Form;
  */
 class RadioField extends ToggleField
 {
+    /**
+     * {@inheritDoc}
+     */
+    public function __construct($key, array $options = array())
+    {
+        $options['type'] = 'radio';
+
+        parent::__construct($key, $options);
+    }
+
     /**
      * {@inheritDoc}
      */
     public function getAttributes()
     {
         return array_merge(parent::getAttributes(), array(
-            'type' => 'radio',
+            // TODO: should getName() be overridden instead?
             'name' => $this->getParent() ? $this->getParent()->getName() : $this->getName(),
         ));
     }

+ 10 - 1
src/Symfony/Component/Form/TextField.php

@@ -18,6 +18,16 @@ namespace Symfony\Component\Form;
  */
 class TextField extends InputField
 {
+    /**
+     * {@inheritDoc}
+     */
+    public function __construct($key, array $options = array())
+    {
+        $options['type'] = 'text';
+
+        parent::__construct($key, $options);
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -34,7 +44,6 @@ class TextField extends InputField
     public function getAttributes()
     {
         return array_merge(parent::getAttributes(), array(
-            'type'        => 'text',
             'maxlength'   => $this->getOption('max_length'),
         ));
     }

+ 2 - 0
src/Symfony/Component/Form/ToggleField.php

@@ -25,6 +25,8 @@ abstract class ToggleField extends InputField
      */
     protected function configure()
     {
+        parent::configure();
+
         $this->addOption('value');
         $this->addOption('label');
 

+ 3 - 1
src/Symfony/Component/Form/UrlField.php

@@ -16,13 +16,15 @@ namespace Symfony\Component\Form;
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  */
-class UrlField extends InputField
+class UrlField extends TextField
 {
     /**
      * {@inheritDoc}
      */
     protected function configure()
     {
+        parent::configure();
+
         $this->addOption('default_protocol', 'http');
     }
 

+ 9 - 0
tests/Symfony/Tests/Component/Form/Fixtures/TestInputField.php

@@ -6,4 +6,13 @@ use Symfony\Component\Form\InputField;
 
 class TestInputField extends InputField
 {
+    /**
+     * {@inheritDoc}
+     */
+    public function __construct($key, array $options = array())
+    {
+        $options['type'] = 'text';
+
+        parent::__construct($key, $options);
+    }
 }

+ 9 - 0
tests/Symfony/Tests/Component/Form/Fixtures/TestToggleField.php

@@ -6,4 +6,13 @@ use Symfony\Component\Form\ToggleField;
 
 class TestToggleField extends ToggleField
 {
+    /**
+     * {@inheritDoc}
+     */
+    public function __construct($key, array $options = array())
+    {
+        $options['type'] = 'checkbox';
+
+        parent::__construct($key, $options);
+    }
 }