[Form] Fix 'invalid_message' use in multiple ChoiceType
This commit is contained in:
parent
20d3d843ad
commit
4f2b492166
|
@ -168,13 +168,14 @@ class ChoiceType extends AbstractType
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($options['multiple']) {
|
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
|
// Throw exception if unknown values were submitted
|
||||||
if (\count($unknownValues) > 0) {
|
if (\count($unknownValues) > 0) {
|
||||||
$form = $event->getForm();
|
$form = $event->getForm();
|
||||||
|
|
||||||
$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : \gettype($form->getViewData());
|
$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : (\is_array($form->getViewData()) ? implode('", "', array_keys($unknownValues)) : \gettype($form->getViewData()));
|
||||||
$messageTemplate = 'The value {{ value }} is not valid.';
|
|
||||||
|
|
||||||
if (null !== $this->translator) {
|
if (null !== $this->translator) {
|
||||||
$message = $this->translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString], 'validators');
|
$message = $this->translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString], 'validators');
|
||||||
|
@ -182,7 +183,7 @@ class ChoiceType extends AbstractType
|
||||||
$message = strtr($messageTemplate, ['{{ value }}' => $clientDataAsString]);
|
$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))));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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\ChoiceGroupView;
|
||||||
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
|
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
|
||||||
|
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
|
||||||
use Symfony\Component\Form\FormInterface;
|
use Symfony\Component\Form\FormInterface;
|
||||||
|
use Symfony\Component\Form\Forms;
|
||||||
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
|
||||||
|
use Symfony\Component\Validator\Validation;
|
||||||
|
|
||||||
class ChoiceTypeTest extends BaseTypeTest
|
class ChoiceTypeTest extends BaseTypeTest
|
||||||
{
|
{
|
||||||
|
@ -1815,6 +1818,38 @@ class ChoiceTypeTest extends BaseTypeTest
|
||||||
$this->assertSame('name[]', $view->vars['full_name']);
|
$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
|
// https://github.com/symfony/symfony/issues/3298
|
||||||
public function testInitializeWithEmptyChoices()
|
public function testInitializeWithEmptyChoices()
|
||||||
{
|
{
|
||||||
|
|
Reference in New Issue