8000 RepeatedType should always have inner types mapped · symfony/symfony@7cfe1fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 7cfe1fe

Browse files
committed
RepeatedType should always have inner types mapped
1 parent 932a4f8 commit 7cfe1fe

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
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;
@@ -36,8 +37,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3637
$options['first_name'],
3738
$options['second_name'],
3839
]))
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']))
40+
->add($options['first_name'], $options['type'], $this->mergeOptions($options['options'], $options['first_options']))
41+
->add($options['second_name'], $options['type'], $this->mergeOptions($options['options'], $options['second_options']))
4142
;
4243
}
4344

@@ -68,4 +69,19 @@ public function getBlockPrefix()
6869
{
6970
return 'repeated';
7071
}
72+
73+
private function mergeOptions(array $fieldOptions, array $innerOptions)
74+
{
75+
$mergedOptions = array_merge($fieldOptions, $innerOptions);
76+
77+
if (\array_key_exists('mapped', $mergedOptions)) {
78+
if (false === $mergedOptions['mapped']) {
79+
throw new InvalidConfigurationException('Inner types must be mapped.');
80+
}
81+
} else {
82+
$mergedOptions['mapped'] = true;
83+
}
84+
85+
return $mergedOptions;
86+
}
7187
}

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

Lines changed: 38 additions & 0 deletions
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,19 @@ 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+
8196
public function testSetInvalidOptions()
8297
{
8398
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
@@ -105,6 +120,29 @@ public function testSetInvalidSecondOptions()
105120
]);
106121
}
107122

123+
/**
124+
* @param string $configurationKey
125+
* @dataProvider notMappedConfigurationKeys
126+
*/
127+
public function testNotMappedInner($configurationKey)
128+
{
129+
$this->expectException(InvalidConfigurationException::class);
130+
$this->expectExceptionMessage('Inner types must be mapped');
131+
132+
$this->factory->create(static::TESTED_TYPE, null, [
133+
'type' => TextTypeTest::TESTED_TYPE,
134+
$configurationKey => ['mapped' => false],
135+
]);
136+
}
137+
138+
public function notMappedConfigurationKeys()
139+
{
140+
return [
141+
['first_options'],
142+
['second_options'],
143+
];
144+
}
145+
108146
public function testSetErrorBubblingToTrue()
109147
{
110148
$form = $this->factory->create(static::TESTED_TYPE, null, [
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)
0