瀏覽代碼

[Form] Adding an exception for an invalid widget option in DateType

Ryan Weaver 14 年之前
父節點
當前提交
78b2062c5e

+ 4 - 1
src/Symfony/Component/Form/Extension/Core/Type/DateType.php

@@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Extension\Core\Type;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormInterface;
 use Symfony\Component\Form\FormBuilder;
+use Symfony\Component\Form\Exception\FormException;
 use Symfony\Component\Form\Extension\Core\ChoiceList\PaddedChoiceList;
 use Symfony\Component\Form\Extension\Core\ChoiceList\MonthChoiceList;
 use Symfony\Component\Form\FormView;
@@ -36,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));
-        } else {
+        } elseif ($options['widget'] == 'choice') {
             // Only pass a subset of the options to children
             $yearOptions = array(
                 'choice_list' => new PaddedChoiceList(
@@ -58,6 +59,8 @@ 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') {

+ 10 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTypeTest.php

@@ -25,6 +25,16 @@ class DateTypeTest extends LocalizedTestCase
         \Locale::setDefault('de_AT');
     }
 
+    /**
+     * @expectedException Symfony\Component\Form\Exception\FormException
+     */
+    public function testInvalidWidgetOption()
+    {
+        $form = $this->factory->create('date', 'name', array(
+            'widget' => 'fake_widget',
+        ));
+    }
+
     public function testSubmitFromInputDateTime()
     {
         $form = $this->factory->create('date', null, array(