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

This commit is contained in:
Bernhard Schussek 2011-01-28 21:52:54 +01:00
parent 916e599937
commit 57cbd57265
5 changed files with 36 additions and 3 deletions

View File

@ -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
{
}

View File

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

View File

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

View File

@ -46,7 +46,7 @@ class Form extends FieldGroup
* @param ValidatorInterface $validator * @param ValidatorInterface $validator
* @param array $options * @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; $this->validator = $validator;

View File

@ -453,6 +453,18 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
$group->add(1234); $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 * @expectedException Symfony\Component\Form\Exception\DanglingFieldException
*/ */