Pārlūkot izejas kodu

[Form] Fields may now be anonymous, but anonymous fields must not be added to groups. They can only be used as prototypes

Bernhard Schussek 14 gadi atpakaļ
vecāks
revīzija
57cbd57265

+ 16 - 0
src/Symfony/Component/Form/Exception/FieldDefinitionException.php

@@ -0,0 +1,16 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Exception;
+
+class FieldDefinitionException extends FormException
+{
+}

+ 1 - 1
src/Symfony/Component/Form/Field.php

@@ -64,7 +64,7 @@ class Field extends Configurable implements FieldInterface
     private $propertyPath = null;
     private $transformationSuccessful = true;
 
-    public function __construct($key, array $options = array())
+    public function __construct($key = null, array $options = array())
     {
         $this->addOption('trim', true);
         $this->addOption('required', true);

+ 6 - 1
src/Symfony/Component/Form/FieldGroup.php

@@ -14,6 +14,7 @@ namespace Symfony\Component\Form;
 use Symfony\Component\Form\Exception\AlreadyBoundException;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\Form\Exception\DanglingFieldException;
+use Symfony\Component\Form\Exception\FieldDefinitionException;
 
 /**
  * FieldGroup represents an array of widgets bind to names and values.
@@ -37,7 +38,7 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
     /**
      * @inheritDoc
      */
-    public function __construct($key, array $options = array())
+    public function __construct($key = null, array $options = array())
     {
         $this->addOption('virtual', false);
 
@@ -121,6 +122,10 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
             $field = $factory->getInstance($this->getData(), $field, $options);
         }
 
+        if ('' === $field->getKey() || null === $field->getKey()) {
+            throw new FieldDefinitionException('You cannot add anonymous fields');
+        }
+
         $this->fields[$field->getKey()] = $field;
 
         $field->setParent($this);

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

@@ -46,7 +46,7 @@ class Form extends FieldGroup
      * @param ValidatorInterface $validator
      * @param array $options
      */
-    public function __construct($name, $data = null, ValidatorInterface $validator = null, array $options = array())
+    public function __construct($name = null, $data = null, ValidatorInterface $validator = null, array $options = array())
     {
         $this->validator = $validator;
 

+ 12 - 0
tests/Symfony/Tests/Component/Form/FieldGroupTest.php

@@ -453,6 +453,18 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
         $group->add(1234);
     }
 
+    /**
+     * @expectedException Symfony\Component\Form\Exception\FormException
+     */
+    public function testAddThrowsExceptionIfAnonymousField()
+    {
+        $group = new TestFieldGroup('author');
+
+        $field = $this->createMockField('');
+
+        $group->add($field);
+    }
+
     /**
      * @expectedException Symfony\Component\Form\Exception\DanglingFieldException
      */