add the ability to set the form prototype name in CollectionType. this will aid in handling nested collections in forms

This commit is contained in:
IamPersistent 2011-10-17 02:43:24 -07:00
parent de9cf88676
commit b53f000061
2 changed files with 28 additions and 6 deletions

View File

@ -25,7 +25,8 @@ class CollectionType extends AbstractType
public function buildForm(FormBuilder $builder, array $options)
{
if ($options['allow_add'] && $options['prototype']) {
$prototype = $builder->create('$$name$$', $options['type'], $options['options']);
$prototypeId = empty($options['prototype_name']) ? '$$name$$' : '$$' . $options['prototype_name'] . '$$';
$prototype = $builder->create($prototypeId, $options['type'], $options['options']);
$builder->setAttribute('prototype', $prototype->getForm());
}
@ -75,11 +76,12 @@ class CollectionType extends AbstractType
public function getDefaultOptions(array $options)
{
return array(
'allow_add' => false,
'allow_delete' => false,
'prototype' => true,
'type' => 'text',
'options' => array(),
'allow_add' => false,
'allow_delete' => false,
'prototype' => true,
'prototype_name' => 'name',
'type' => 'text',
'options' => array(),
);
}

View File

@ -166,4 +166,24 @@ class CollectionFormTest extends TypeTestCase
$data = $form->getData();
$this->assertFalse(isset($data['$$name$$']));
}
public function testPrototypeNameOption()
{
$form = $this->factory->create('collection', null, array(
'type' => 'field',
'prototype' => true,
'allow_add' => true,
));
$this->assertSame('$$name$$', $form->getAttribute('prototype')->getName(), '$$name$$ is the default');
$form = $this->factory->create('collection', null, array(
'type' => 'field',
'prototype' => true,
'allow_add' => true,
'prototype_name' => 'test',
));
$this->assertSame('$$test$$', $form->getAttribute('prototype')->getName());
}
}