Browse Source

[Form] Implemented getAllowedOptionValues() for core types

Bernhard Schussek 14 years ago
parent
commit
7570e04589

+ 29 - 4
src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

@@ -55,9 +55,6 @@ class DateTimeType extends AbstractType
         if (isset($options['time_widget'])) {
             $timeOptions['widget'] = $options['time_widget'];
         }
-        if (isset($options['time_format'])) {
-            $timeOptions['format'] = $options['time_format'];
-        }
 
         $timeOptions['input'] = 'array';
 
@@ -109,7 +106,6 @@ class DateTimeType extends AbstractType
             'date_format' => null,
             'time_pattern' => null,
             'time_widget' => null,
-            'time_format' => null,
             /* Defaults for date field */
             'years' => range(date('Y') - 5, date('Y') + 5),
             'months' => range(1, 12),
@@ -122,6 +118,35 @@ class DateTimeType extends AbstractType
         );
     }
 
+    public function getAllowedOptionValues(array $options)
+    {
+        return array(
+            'input' => array(
+                'datetime',
+                'string',
+                'timestamp',
+                'array',
+            ),
+            'date_widget' => array(
+                null, // inherit default from DateType
+                'text',
+                'choice',
+            ),
+            'date_format' => array(
+                null, // inherit default from DateType
+                \IntlDateFormatter::FULL,
+                \IntlDateFormatter::LONG,
+                \IntlDateFormatter::MEDIUM,
+                \IntlDateFormatter::SHORT,
+             ),
+            'time_widget' => array(
+                null, // inherit default from TimeType
+                'text',
+                'choice',
+            ),
+        );
+    }
+
     public function getName()
     {
         return 'datetime';

+ 23 - 3
src/Symfony/Component/Form/Extension/Core/Type/DateType.php

@@ -37,7 +37,7 @@ class DateType extends AbstractType
 
         if ($options['widget'] === 'text') {
             $builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE));
-        } elseif ($options['widget'] == 'choice') {
+        } else {
             // Only pass a subset of the options to children
             $yearOptions = array(
                 'choice_list' => new PaddedChoiceList(
@@ -59,8 +59,6 @@ class DateType extends AbstractType
                 ->add('month', 'choice', $monthOptions)
                 ->add('day', 'choice', $dayOptions)
                 ->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day')));
-        } else {
-            throw new FormException('The "widget" option must be set to either "text" or "choice".');
         }
 
         if ($options['input'] === 'string') {
@@ -124,6 +122,28 @@ class DateType extends AbstractType
         );
     }
 
+    public function getAllowedOptionValues(array $options)
+    {
+        return array(
+            'input' => array(
+                'datetime',
+                'string',
+                'timestamp',
+                'array',
+            ),
+            'widget' => array(
+                'text',
+                'choice',
+            ),
+            'format' => array(
+                \IntlDateFormatter::FULL,
+                \IntlDateFormatter::LONG,
+                \IntlDateFormatter::MEDIUM,
+                \IntlDateFormatter::SHORT,
+             ),
+        );
+    }
+
     public function getParent(array $options)
     {
         return $options['widget'] === 'text' ? 'field' : 'form';

+ 10 - 0
src/Symfony/Component/Form/Extension/Core/Type/FileType.php

@@ -61,6 +61,16 @@ class FileType extends AbstractType
         );
     }
 
+    public function getAllowedOptionValues(array $options)
+    {
+        return array(
+            'type' => array(
+                'string',
+                'file',
+            ),
+        );
+    }
+
     public function getName()
     {
         return 'file';

+ 16 - 1
src/Symfony/Component/Form/Extension/Core/Type/IntegerType.php

@@ -29,7 +29,22 @@ class IntegerType extends AbstractType
             'precision' => null,
             'grouping' => false,
             // Integer cast rounds towards 0, so do the same when displaying fractions
-            'rounding_mode' => IntegerToLocalizedStringTransformer::ROUND_DOWN,
+            'rounding_mode' => \NumberFormatter::ROUND_DOWN,
+        );
+    }
+
+    public function getAllowedOptionValues(array $options)
+    {
+        return array(
+            'rounding_mode' => array(
+                \NumberFormatter::ROUND_FLOOR,
+                \NumberFormatter::ROUND_DOWN,
+                \NumberFormatter::ROUND_HALFDOWN,
+                \NumberFormatter::ROUND_HALFEVEN,
+                \NumberFormatter::ROUND_HALFUP,
+                \NumberFormatter::ROUND_UP,
+                \NumberFormatter::ROUND_CEILING,
+            ),
         );
     }
 

+ 16 - 1
src/Symfony/Component/Form/Extension/Core/Type/NumberType.php

@@ -28,7 +28,22 @@ class NumberType extends AbstractType
             // default precision is locale specific (usually around 3)
             'precision' => null,
             'grouping' => false,
-            'rounding_mode' => NumberToLocalizedStringTransformer::ROUND_HALFUP,
+            'rounding_mode' => \NumberFormatter::ROUND_HALFUP,
+        );
+    }
+
+    public function getAllowedOptionValues(array $options)
+    {
+        return array(
+            'rounding_mode' => array(
+                \NumberFormatter::ROUND_FLOOR,
+                \NumberFormatter::ROUND_DOWN,
+                \NumberFormatter::ROUND_HALFDOWN,
+                \NumberFormatter::ROUND_HALFEVEN,
+                \NumberFormatter::ROUND_HALFUP,
+                \NumberFormatter::ROUND_UP,
+                \NumberFormatter::ROUND_CEILING,
+            ),
         );
     }
 

+ 10 - 0
src/Symfony/Component/Form/Extension/Core/Type/PercentType.php

@@ -30,6 +30,16 @@ class PercentType extends AbstractType
         );
     }
 
+    public function getAllowedOptionValues(array $options)
+    {
+        return array(
+            'type' => array(
+                'fractional',
+                'integer',
+            ),
+        );
+    }
+
     public function getParent(array $options)
     {
         return 'field';

+ 16 - 0
src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

@@ -96,6 +96,22 @@ class TimeType extends AbstractType
         );
     }
 
+    public function getAllowedOptionValues(array $options)
+    {
+        return array(
+            'input' => array(
+                'datetime',
+                'string',
+                'timestamp',
+                'array',
+            ),
+            'widget' => array(
+                'text',
+                'choice',
+            ),
+        );
+    }
+
     public function getName()
     {
         return 'time';