8000 [Form] Fix 'invalid_message' use in multiple ChoiceType · shuvroroy/symfony@4f2b492 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f2b492

Browse files
alexandre-dauboisxabbuh
authored andcommitted
[Form] Fix 'invalid_message' use in multiple ChoiceType
1 parent 20d3d84 commit 4f2b492

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
169
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
168168
}
169

170170
if ($options['multiple']) {
171-
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues) {
171+
$messageTemplate = $options['invalid_message'] ?? 'The value {{ value }} is not valid.';
172+
173+
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues, $messageTemplate) {
172174
// Throw exception if unknown values were submitted
173175
if (\count($unknownValues) > 0) {
174176
$form = $event->getForm();
175177

176-
$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : \gettype($form->getViewData());
177-
$messageTemplate = 'The value {{ value }} is not valid.';
178+
$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : (\is_array($form->getViewData()) ? implode('", "', array_keys($unknownValues)) : \gettype($form->getViewData()));
178179

179180
if (null !== $this->translator) {
180181
$message = $this->translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString], 'validators');
181182
} else {
182183
$message = strtr($messageTemplate, ['{{ value }}' => $clientDataAsString]);
183184
}
184185

185-
$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))))));
186+
$form->addError(new FormError($message, $messageTemplate, ['{{ value }}' => $clientDataAsString], null, new TransformationFailedException(sprintf('The choices "%s" do not exist in the choice list.', $clientDataAsString))));
186187
}
187188
});
188189

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
1515
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
16+
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
1617
use Symfony\Component\Form\FormInterface;
18+
use Symfony\Component\Form\Forms;
1719
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
20+
use Symfony\Component\Validator\Validation;
1821

1922
class ChoiceTypeTest extends BaseTypeTest
2023
{
@@ -1815,6 +1818,38 @@ public function testAdjustFullNameForMultipleNonExpanded()
18151818
$this->assertSame('name[]', $view->vars['full_name']);
18161819
}
18171820

1821+
public function testInvalidMessageAwarenessForMultiple()
1822+
{
1823+
$factory = Forms::createFormFactoryBuilder()
1824+
->addExtensions([new ValidatorExtension(Validation::createValidator())])
1825+
->getFormFactory();
1826+
$form = $factory->create(static::TESTED_TYPE, null, [
1827+
'multiple' => true,
1828+
'expanded' => false,
1829+
'choices' => $this->choices,
1830+
'invalid_message' => 'You are not able to use value "{{ value }}"',
1831+
]);
1832+
1833+
$form->submit(['My invalid choice']);
1834+
$this->assertEquals("ERROR: You are not able to use value \"My invalid choice\"\n", (string) $form->getErrors(true));
1835+
}
1836+
1837+
public function testInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData()
1838+
{
1839+
$factory = Forms::createFormFactoryBuilder()
1840+
->addExtensions([new ValidatorExtension(Validation::createValidator())])
1841+
->getFormFactory();
1842+
$form = $factory->create(static::TESTED_TYPE, null, [
1843+
'multiple' => true,
1844+
'expanded' => false,
1845+
'choices' => $this->choices,
1846+
'invalid_message' => 'You are not able to use value "{{ value }}"',
1847+
]);
1848+
1849+
$form->submit(new \stdClass());
1850+
$this->assertEquals("ERROR: You are not able to use value \"object\"\n", (string) $form->getErrors(true));
1851+
}
1852+
18181853
// https://github.com/symfony/symfony/issues/3298
18191854
public function testInitializeWithEmptyChoices()
18201855
{

0 commit comments

Comments
 (0)
0