Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] RepeatedType should always have inner types mapped #36411

Merged
merged 1 commit into from Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,13 +31,16 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$options['options']['error_bubbling'] = $options['error_bubbling'];
}

// children fields must always be mapped
$defaultOptions = ['mapped' => true];

$builder
->addViewTransformer(new ValueToDuplicatesTransformer([
$options['first_name'],
$options['second_name'],
]))
->add($options['first_name'], $options['type'], array_merge($options['options'], $options['first_options']))
->add($options['second_name'], $options['type'], array_merge($options['options'], $options['second_options']))
->add($options['first_name'], $options['type'], array_merge($options['options'], $options['first_options'], $defaultOptions))
->add($options['second_name'], $options['type'], array_merge($options['options'], $options['second_options'], $defaultOptions))
;
}

Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\Tests\Fixtures\NotMappedType;

class RepeatedTypeTest extends BaseTypeTest
{
Expand Down Expand Up @@ -78,6 +79,41 @@ public function testSetRequired()
$this->assertFalse($form['second']->isRequired());
}

public function testMappedOverridesDefault()
{
$form = $this->factory->create(NotMappedType::class);
$this->assertFalse($form->getConfig()->getMapped());

$form = $this->factory->create(static::TESTED_TYPE, null, [
'type' => NotMappedType::class,
]);

$this->assertTrue($form['first']->getConfig()->getMapped());
$this->assertTrue($form['second']->getConfig()->getMapped());
}

/**
* @dataProvider notMappedConfigurationKeys
*/
public function testNotMappedInnerIsOverridden($configurationKey)
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'type' => TextTypeTest::TESTED_TYPE,
$configurationKey => ['mapped' => false],
]);

$this->assertTrue($form['first']->getConfig()->getMapped());
$this->assertTrue($form['second']->getConfig()->getMapped());
}

public function notMappedConfigurationKeys()
{
return [
['first_options'],
['second_options'],
];
}

public function testSetInvalidOptions()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/Form/Tests/Fixtures/NotMappedType.php
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NotMappedType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('mapped', false);
}
}