10000 [Form] validate non-mapped repeated types by xabbuh · Pull Request #36430 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] validate non-mapped repeated types #36430

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/RepeatedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToDuplicatesTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RepeatedType extends AbstractType
Expand All @@ -31,11 +34,33 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$options['options']['error_bubbling'] = $options['error_bubbling'];
}

$submittedData = [];

$builder
->addViewTransformer(new ValueToDuplicatesTransformer([
$options['first_name'],
$options['second_name'],
]))
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use (&$submittedData) {
$submittedData = $event->getData();
})
->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) use ($options, &$submittedData) {
$isEmpty = function (array $data, $key) {
return !isset($data[$key]) || '' === $data[$key] || false === $data[$key] || [] === $data[$key];
};

if ($isEmpty($submittedData, $options['first_name']) && !$isEmpty($submittedData, $options['second_name'])) {
throw new TransformationFailedException(sprintf('The key "%s" should not be empty.', $options['first_name']));
}

if (!$isEmpty($submittedData, $options['first_name']) && $isEmpty($submittedData, $options['second_name'])) {
throw new TransformationFailedException(sprintf('The key "%s" should not be empty.', $options['second_name']));
}

if ($submittedData[$options['first_name']] !== $submittedData[$options['second_name']]) {
throw new TransformationFailedException('All values in the array should be the same.');
}
})
->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']))
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,32 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
{
parent::testSubmitNull($expected, $norm, ['first' => null, 7C60 'second' => null]);
}

/**
* @dataProvider provideNotEqualData
*/
public function testSubmitNotEqualValuesWithNotMappedFields($submittedFirstData, $submittedSecondData)
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'type' => TextTypeTest::TESTED_TYPE,
'first_options' => ['mapped' => false],
'second_options' => ['mapped' => false],
]);

$form->submit(['first' => $submittedFirstData, 'second' => $submittedSecondData]);

$this->assertSame($submittedFirstData, $form['first']->getViewData());
$this->assertSame($submittedSecondData, $form['second']->getViewData());
$this->assertFalse($form->isSynchronized());
$this->assertNull($form->getData());
}

public function provideNotEqualData()
{
return [
['foo', 'bar'],
['', 'bar'],
['foo', ''],
];
}
}
0