Procházet zdrojové kódy

[Form] Adding a check that the choice_list option on the ChoiceType implements the ChoiceListInterface.

Also removed an unused "use" statement.
Ryan Weaver před 14 roky
rodič
revize
967a42b797

+ 5 - 1
src/Symfony/Component/Form/Type/ChoiceType.php

@@ -14,8 +14,8 @@ namespace Symfony\Component\Form\Type;
 use Symfony\Component\Form\FormBuilder;
 use Symfony\Component\Form\FormInterface;
 use Symfony\Component\Form\Exception\FormException;
-use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
+use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
 use Symfony\Component\Form\EventListener\FixRadioInputListener;
 use Symfony\Component\Form\FormView;
 use Symfony\Component\Form\DataTransformer\ScalarToChoiceTransformer;
@@ -31,6 +31,10 @@ class ChoiceType extends AbstractType
             throw new FormException('Either the option "choices" or "choice_list" is required');
         }
 
+        if ($options['choice_list'] && !$options['choice_list'] instanceof ChoiceListInterface) {
+            throw new FormException('The "choice_list" must be an instance of "Symfony\Component\Form\ChoiceList\ChoiceListInterface".');
+        }
+
         if (!$options['choice_list']) {
             $options['choice_list'] = new ArrayChoiceList($options['choices']);
         }

+ 10 - 0
tests/Symfony/Tests/Component/Form/Type/ChoiceTypeTest.php

@@ -56,6 +56,16 @@ class ChoiceTypeTest extends TestCase
         ));
     }
 
+    /**
+     * @expectedException Symfony\Component\Form\Exception\FormException
+     */
+    public function testChoiceListOptionExpectsChoiceListInterface()
+    {
+        $form = $this->factory->create('choice', 'name', array(
+            'choice_list' => array('foo' => 'foo'),
+        ));
+    }
+
     public function testExpandedCheckboxesAreNeverRequired()
     {
         $form = $this->factory->create('choice', 'name', array(