bug #16959 [Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too (HeahDude)

This PR was squashed before being merged into the 2.3 branch (closes #16959).

Discussion
----------

[Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15544
| License       | MIT
| Doc PR        | -

Commits
-------

b4b5d63 [Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too
This commit is contained in:
Fabien Potencier 2015-12-18 17:43:49 +01:00
commit baa5b7db29
2 changed files with 27 additions and 0 deletions

View File

@ -28,6 +28,7 @@ class CollectionType extends AbstractType
{
if ($options['allow_add'] && $options['prototype']) {
$prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array(
'required' => $options['required'],
'label' => $options['prototype_name'].'label__',
), $options['options']));
$builder->setAttribute('prototype', $prototype->getForm());

View File

@ -195,4 +195,30 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertSame('__test__label__', $form->createView()->vars['prototype']->vars['label']);
}
public function testPrototypeDefaultRequired()
{
$form = $this->factory->create('collection', array(), array(
'type' => 'file',
'allow_add' => true,
'prototype' => true,
'prototype_name' => '__test__',
));
$this->assertTrue($form->createView()->vars['prototype']->vars['required']);
}
public function testPrototypeSetNotRequired()
{
$form = $this->factory->create('collection', array(), array(
'type' => 'file',
'allow_add' => true,
'prototype' => true,
'prototype_name' => '__test__',
'required' => false,
));
$this->assertFalse($form->createView()->vars['required'], 'collection is not required');
$this->assertFalse($form->createView()->vars['prototype']->vars['required'], '"prototype" should not be required');
}
}