Skip to content

Commit 134a7c9

Browse files
committed
bug symfony#18317 [Form] fix "prototype" not required when parent form is not required (HeahDude)
This PR was merged into the 2.3 branch. Discussion ---------- [Form] fix "prototype" not required when parent form is not required | Q | A | ------------- | --- | Branch? | 2.3+ | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#18311 | License | MIT | Doc PR | ~ Commits ------- 7df9ca2 [Form] fix "prototype" not required when parent form is not required
2 parents 019e316 + 7df9ca2 commit 134a7c9

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
5555
));
5656

5757
if ($form->getConfig()->hasAttribute('prototype')) {
58-
$view->vars['prototype'] = $form->getConfig()->getAttribute('prototype')->createView($view);
58+
$prototype = $form->getConfig()->getAttribute('prototype');
59+
$view->vars['prototype'] = $prototype->setParent($form)->createView($view);
5960
}
6061
}
6162

src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,46 @@ public function testPrototypeSetNotRequired()
221221
$this->assertFalse($form->createView()->vars['required'], 'collection is not required');
222222
$this->assertFalse($form->createView()->vars['prototype']->vars['required'], '"prototype" should not be required');
223223
}
224+
225+
public function testPrototypeSetNotRequiredIfParentNotRequired()
226+
{
227+
$child = $this->factory->create('collection', array(), array(
228+
'type' => 'file',
229+
'allow_add' => true,
230+
'prototype' => true,
231+
'prototype_name' => '__test__',
232+
));
233+
234+
$parent = $this->factory->create('form', array(), array(
235+
'required' => false,
236+
));
237+
238+
$child->setParent($parent);
239+
$this->assertFalse($parent->createView()->vars['required'], 'Parent is not required');
240+
$this->assertFalse($child->createView()->vars['required'], 'Child is not required');
241+
$this->assertFalse($child->createView()->vars['prototype']->vars['required'], '"Prototype" should not be required');
242+
}
243+
244+
public function testPrototypeNotOverrideRequiredByEntryOptionsInFavorOfParent()
245+
{
246+
$child = $this->factory->create('collection', array(), array(
247+
'type' => 'file',
248+
'allow_add' => true,
249+
'prototype' => true,
250+
'prototype_name' => '__test__',
251+
'options' => array(
252+
'required' => true,
253+
),
254+
));
255+
256+
$parent = $this->factory->create('form', array(), array(
257+
'required' => false,
258+
));
259+
260+
$child->setParent($parent);
261+
262+
$this->assertFalse($parent->createView()->vars['required'], 'Parent is not required');
263+
$this->assertFalse($child->createView()->vars['required'], 'Child is not required');
264+
$this->assertFalse($child->createView()->vars['prototype']->vars['required'], '"Prototype" should not be required');
265+
}
224266
}

0 commit comments

Comments
 (0)