Skip to content

Commit dc6b28a

Browse files
bug #36411 [Form] RepeatedType should always have inner types mapped (biozshock)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] RepeatedType should always have inner types mapped | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Doc PR| symfony/symfony-docs#13519 | | Tickets | Fix #36410 | License | MIT Always set mapped=true to override inner type mapped setting. Throw an exception if inner types of RepeatedType has mapped=false Commits ------- 728cd66a13 RepeatedType should always have inner types mapped
2 parents 923082c + 3f400e5 commit dc6b28a

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

Extension/Core/Type/RepeatedType.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3131
$options['options']['error_bubbling'] = $options['error_bubbling'];
3232
}
3333

34+
// children fields must always be mapped
35+
$defaultOptions = ['mapped' => true];
36+
3437
$builder
3538
->addViewTransformer(new ValueToDuplicatesTransformer([
3639
$options['first_name'],
3740
$options['second_name'],
3841
]))
39-
->add($options['first_name'], $options['type'], array_merge($options['options'], $options['first_options']))
40-
->add($options['second_name'], $options['type'], array_merge($options['options'], $options['second_options']))
42+
->add($options['first_name'], $options['type'], array_merge($options['options'], $options['first_options'], $defaultOptions))
43+
->add($options['second_name'], $options['type'], array_merge($options['options'], $options['second_options'], $defaultOptions))
4144
;
4245
}
4346

Tests/Extension/Core/Type/RepeatedTypeTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\Form;
15+
use Symfony\Component\Form\Tests\Fixtures\NotMappedType;
1516

1617
class RepeatedTypeTest extends BaseTypeTest
1718
{
@@ -78,6 +79,41 @@ public function testSetRequired()
7879
$this->assertFalse($form['second']->isRequired());
7980
}
8081

82+
public function testMappedOverridesDefault()
83+
{
84+
$form = $this->factory->create(NotMappedType::class);
85+
$this->assertFalse($form->getConfig()->getMapped());
86+
87+
$form = $this->factory->create(static::TESTED_TYPE, null, [
88+
'type' => NotMappedType::class,
89+
]);
90+
91+
$this->assertTrue($form['first']->getConfig()->getMapped());
92+
$this->assertTrue($form['second']->getConfig()->getMapped());
93+
}
94+
95+
/**
96+
* @dataProvider notMappedConfigurationKeys
97+
*/
98+
public function testNotMappedInnerIsOverridden($configurationKey)
99+
{
100+
$form = $this->factory->create(static::TESTED_TYPE, null, [
101+
'type' => TextTypeTest::TESTED_TYPE,
102+
$configurationKey => ['mapped' => false],
103+
]);
104+
105+
$this->assertTrue($form['first']->getConfig()->getMapped());
106+
$this->assertTrue($form['second']->getConfig()->getMapped());
107+
}
108+
109+
public function notMappedConfigurationKeys()
110+
{
111+
return [
112+
['first_options'],
113+
['second_options'],
114+
];
115+
}
116+
81117
public function testSetInvalidOptions()
82118
{
83119
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');

Tests/Fixtures/NotMappedType.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
class NotMappedType extends AbstractType
18+
{
19+
public function configureOptions(OptionsResolver $resolver)
20+
{
21+
$resolver->setDefault('mapped', false);
22+
}
23+
}

0 commit comments

Comments
 (0)