Skip to content

Commit 9af2203

Browse files
committed
RepeatedType should always have inner types mapped
1 parent 932a4f8 commit 9af2203

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\Exception\InvalidConfigurationException;
1516
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToDuplicatesTransformer;
1617
use Symfony\Component\Form\FormBuilderInterface;
1718
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -31,13 +32,24 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3132
$options['options']['error_bubbling'] = $options['error_bubbling'];
3233
}
3334

35+
// children fields must always be mapped
36+
$defaultOptions = ['mapped' => true];
37+
3438
$builder
3539
->addViewTransformer(new ValueToDuplicatesTransformer([
3640
$options['first_name'],
3741
$options['second_name'],
3842
]))
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']))
43+
->add(
44+
$options['first_name'],
45+
$options['type'],
46+
array_merge($options['options'], $options['first_options'], $defaultOptions)
47+
)
48+
->add(
49+
$options['second_name'],
50+
$options['type'],
51+
array_merge($options['options'], $options['second_options'], $defaultOptions)
52+
)
4153
;
4254
}
4355

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

+38
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

14+
use Symfony\Component\Form\Exception\InvalidConfigurationException;
1415
use Symfony\Component\Form\Form;
16+
use Symfony\Component\Form\Tests\Fixtures\NotMappedType;
1517

1618
class RepeatedTypeTest extends BaseTypeTest
1719
{
@@ -78,6 +80,42 @@ public function testSetRequired()
7880
$this->assertFalse($form['second']->isRequired());
7981
}
8082

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