[Form] Changed FormBuilder::build() to FormBuilder::create(). You hvae to pass the resulting builder to FormBuilder::add() manually now

$builder->add(
    $builder->create('child', 'form')
        ->add('foo', 'text')
        ->add('bar', 'text')
);
This commit is contained in:
Bernhard Schussek 2011-04-24 10:54:50 +02:00
parent 4f2fee8190
commit 675e5ded9e
2 changed files with 19 additions and 41 deletions

View File

@ -40,8 +40,6 @@ class FormBuilder
private $types = array(); private $types = array();
private $parent;
private $dataClass; private $dataClass;
private $children = array(); private $children = array();
@ -70,23 +68,6 @@ class FormBuilder
return $this->name; return $this->name;
} }
public function setParent(FormBuilder $builder)
{
$this->parent = $builder;
return $this;
}
public function getParent()
{
return $this->parent;
}
public function end()
{
return $this->parent;
}
public function setData($data) public function setData($data)
{ {
$this->data = $data; $this->data = $data;
@ -341,17 +322,23 @@ class FormBuilder
* @param array $options * @param array $options
* @return FormInterface * @return FormInterface
*/ */
public function add($name, $type = null, array $options = array()) public function add($child, $type = null, array $options = array())
{ {
if (!is_string($name)) { if ($child instanceof self) {
throw new UnexpectedTypeException($name, 'string'); $this->children[$child->getName()] = $child;
return $this;
}
if (!is_string($child)) {
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormBuilder');
} }
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) { if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface'); throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
} }
$this->children[$name] = array( $this->children[$child] = array(
'type' => $type, 'type' => $type,
'options' => $options, 'options' => $options,
); );
@ -359,7 +346,7 @@ class FormBuilder
return $this; return $this;
} }
public function build($name, $type = null, array $options = array()) public function create($name, $type = null, array $options = array())
{ {
if (null !== $type) { if (null !== $type) {
$builder = $this->getFormFactory()->createNamedBuilder( $builder = $this->getFormFactory()->createNamedBuilder(
@ -381,10 +368,6 @@ class FormBuilder
); );
} }
$this->children[$name] = $builder;
$builder->setParent($this);
return $builder; return $builder;
} }
@ -394,13 +377,13 @@ class FormBuilder
throw new FormException(sprintf('The field "%s" does not exist', $name)); throw new FormException(sprintf('The field "%s" does not exist', $name));
} }
$child = $this->children[$name]; if (!$this->children[$name] instanceof FormBuilder) {
$this->children[$name] = $this->create($name,
if ($child instanceof FormBuilder) { $this->children[$name]['type'],
return $child; $this->children[$name]['options']);
} }
return $this->build($name, $child['type'], $child['options']); return $this->children[$name];
} }
/** /**
@ -411,11 +394,6 @@ class FormBuilder
public function remove($name) public function remove($name)
{ {
if (isset($this->children[$name])) { if (isset($this->children[$name])) {
// field might still be lazy
if ($this->children[$name] instanceof FormInterface) {
$this->children[$name]->setParent(null);
}
unset($this->children[$name]); unset($this->children[$name]);
} }
} }
@ -442,7 +420,7 @@ class FormBuilder
foreach ($this->children as $name => $builder) { foreach ($this->children as $name => $builder) {
if (!$builder instanceof FormBuilder) { if (!$builder instanceof FormBuilder) {
$builder = $this->build($name, $builder['type'], $builder['options']); $builder = $this->create($name, $builder['type'], $builder['options']);
} }
$children[$builder->getName()] = $builder->getForm(); $children[$builder->getName()] = $builder->getForm();

View File

@ -95,10 +95,10 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($this->builder->has('foo')); $this->assertFalse($this->builder->has('foo'));
} }
public function testBuildNoTypeNoDataClass() public function testCreateNoTypeNoDataClass()
{ {
$this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The data class must be set to automatically create children'); $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The data class must be set to automatically create children');
$this->builder->build('foo'); $this->builder->create('foo');
} }
public function testGetUnknown() public function testGetUnknown()