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:
parent
de9cf88676
commit
b53f000061
@ -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(),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user