[Form] Fix 'invalid_message' use in multiple ChoiceType

This commit is contained in:
Alexandre Daubois 2021-03-31 19:23:01 +02:00 committed by Christian Flothmann
parent 20d3d843ad
commit 4f2b492166
2 changed files with 40 additions and 4 deletions

View File

@ -168,13 +168,14 @@ class ChoiceType extends AbstractType
}
if ($options['multiple']) {
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues) {
$messageTemplate = $options['invalid_message'] ?? 'The value {{ value }} is not valid.';
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues, $messageTemplate) {
// Throw exception if unknown values were submitted
if (\count($unknownValues) > 0) {
$form = $event->getForm();
$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : \gettype($form->getViewData());
$messageTemplate = 'The value {{ value }} is not valid.';
$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : (\is_array($form->getViewData()) ? implode('", "', array_keys($unknownValues)) : \gettype($form->getViewData()));
if (null !== $this->translator) {
$message = $this->translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString], 'validators');
@ -182,7 +183,7 @@ class ChoiceType extends AbstractType
$message = strtr($messageTemplate, ['{{ value }}' => $clientDataAsString]);
}
$form->addError(new FormError($message, $messageTemplate, ['{{ value }}' => $clientDataAsString], null, new TransformationFailedException(sprintf('The choices "%s" do not exist in the choice list.', implode('", "', array_keys($unknownValues))))));
$form->addError(new FormError($message, $messageTemplate, ['{{ value }}' => $clientDataAsString], null, new TransformationFailedException(sprintf('The choices "%s" do not exist in the choice list.', $clientDataAsString))));
}
});

View File

@ -13,8 +13,11 @@ namespace Symfony\Component\Form\Tests\Extension\Core\Type;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Forms;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\Validator\Validation;
class ChoiceTypeTest extends BaseTypeTest
{
@ -1815,6 +1818,38 @@ class ChoiceTypeTest extends BaseTypeTest
$this->assertSame('name[]', $view->vars['full_name']);
}
public function testInvalidMessageAwarenessForMultiple()
{
$factory = Forms::createFormFactoryBuilder()
->addExtensions([new ValidatorExtension(Validation::createValidator())])
->getFormFactory();
$form = $factory->create(static::TESTED_TYPE, null, [
'multiple' => true,
'expanded' => false,
'choices' => $this->choices,
'invalid_message' => 'You are not able to use value "{{ value }}"',
]);
$form->submit(['My invalid choice']);
$this->assertEquals("ERROR: You are not able to use value \"My invalid choice\"\n", (string) $form->getErrors(true));
}
public function testInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData()
{
$factory = Forms::createFormFactoryBuilder()
->addExtensions([new ValidatorExtension(Validation::createValidator())])
->getFormFactory();
$form = $factory->create(static::TESTED_TYPE, null, [
'multiple' => true,
'expanded' => false,
'choices' => $this->choices,
'invalid_message' => 'You are not able to use value "{{ value }}"',
]);
$form->submit(new \stdClass());
$this->assertEquals("ERROR: You are not able to use value \"object\"\n", (string) $form->getErrors(true));
}
// https://github.com/symfony/symfony/issues/3298
public function testInitializeWithEmptyChoices()
{