Quellcode durchsuchen

[Form] Added options validation to field creation

Bernhard Schussek vor 14 Jahren
Ursprung
Commit
7585dce8dd

+ 16 - 2
src/Symfony/Component/Form/FormFactory.php

@@ -15,6 +15,7 @@ use Symfony\Component\Form\Type\FormTypeInterface;
 use Symfony\Component\Form\Type\Loader\TypeLoaderInterface;
 use Symfony\Component\Form\Type\Guesser\TypeGuesserInterface;
 use Symfony\Component\Form\Type\Guesser\Guess;
+use Symfony\Component\Form\Exception\FormException;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 
 class FormFactory implements FormFactoryInterface
@@ -40,6 +41,8 @@ class FormFactory implements FormFactoryInterface
 
         $builder = null;
         $types = array();
+        $knownOptions = array();
+        $passedOptions = array_keys($options);
 
         // TESTME
         if (null === $name && preg_match('/\w+$/', $type, $matches)) {
@@ -50,11 +53,22 @@ class FormFactory implements FormFactoryInterface
             // TODO check if type exists
             $type = $this->typeLoader->getType($type);
             array_unshift($types, $type);
-            $options = array_merge($type->getDefaultOptions($options), $options);
-            $builder = $builder ?: $type->createBuilder($options);
+            $defaultOptions = $type->getDefaultOptions($options);
+            $options = array_merge($defaultOptions, $options);
+            $knownOptions = array_merge($knownOptions, array_keys($defaultOptions));
             $type = $type->getParent($options);
         }
 
+        $diff = array_diff($passedOptions, $knownOptions);
+
+        if (count($diff) > 0) {
+            throw new FormException(sprintf('The options "%s" do not exist', implode('", "', $diff)));
+        }
+
+        for ($i = 0, $l = count($types); $i < $l && !$builder; ++$i) {
+            $builder = $types[$i]->createBuilder($options);
+        }
+
         // TODO check if instance exists
 
         $builder->setName($name);

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

@@ -65,6 +65,7 @@ class ChoiceType extends AbstractType
             'choices' => array(),
             'preferred_choices' => array(),
             'csrf_protection' => false,
+            'choice_list' => null,
         );
 
         $options = array_replace($defaultOptions, $options);

+ 6 - 0
src/Symfony/Component/Form/Type/DateTimeType.php

@@ -104,6 +104,12 @@ class DateTimeType extends AbstractType
             // Don't modify \DateTime classes by reference, we treat
             // them like immutable value objects
             'by_reference' => false,
+            'date_pattern' => null,
+            'date_widget' => null,
+            'date_format' => null,
+            'time_pattern' => null,
+            'time_widget' => null,
+            'time_format' => null,
         );
     }
 

+ 1 - 0
src/Symfony/Component/Form/Type/RepeatedType.php

@@ -35,6 +35,7 @@ class RepeatedType extends AbstractType
             'first_name' => 'first',
             'second_name' => 'second',
             'csrf_protection' => false,
+            'error_bubbling' => false,
         );
     }
 

+ 2 - 2
tests/Symfony/Tests/Component/Form/Renderer/Theme/AbstractThemeFunctionalTest.php

@@ -112,7 +112,7 @@ class MyTestFormConfig extends AbstractType
     {
         $builder->setDataClass('Symfony\Bundle\FrameworkBundle\Tests\Form\MyTestObject');
         $builder->add('field0', 'my.sub_form');
-        $builder->add('field1', 'text', array('max_length' => 127, 'id' => 'foo'));
+        $builder->add('field1', 'text', array('max_length' => 127));
         $builder->add('field2', 'date');
         $builder->add('field5', 'time');
         $builder->add('field3', 'choice', array(
@@ -144,7 +144,7 @@ class MyTestFormConfig extends AbstractType
         $builder->add('field15', 'password');
         $builder->add('field16', 'percent');
         $builder->add('field17', 'radio');
-        $builder->add('field18', 'repeated', array('identifier' => 'password'));
+        $builder->add('field18', 'repeated', array('type' => 'password'));
         $builder->add('emails', 'collection', array(
             'type' => 'text',
         ));

+ 1 - 1
tests/Symfony/Tests/Component/Form/Type/RepeatedTypeTest.php

@@ -25,7 +25,7 @@ class RepeatedTypeTest extends TestCase
         parent::setUp();
 
         $this->form = $this->factory->create('repeated', 'name', array(
-            'prototype' => $this->factory->create('field', 'foo'),
+            'type' => 'field',
         ));
         $this->form->setData(null);
     }