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

Skip to content

Commit e3c3795

Browse files
[Form] Fix 'invalid_message' use in multiple ChoiceType
1 parent f615903 commit e3c3795

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,25 @@ public function buildForm(FormBuilderInterface $builder, array $options)
187187
}
188188

189189
if ($options['multiple']) {
190-
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues) {
190+
$userInvalidMessage = $options['invalid_message'] ?? null;
191+
192+
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues, $userInvalidMessage) {
191193
// Throw exception if unknown values were submitted
192194
if (\count($unknownValues) > 0) {
193195
$form = $event->getForm();
194196

195-
$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : \gettype($form->getViewData());
196-
$messageTemplate = 'The value {{ value }} is not valid.';
197+
$clientDataAsString = is_scalar($form->getViewData()) ?
198+
(string) $form->getViewData() :
199+
(\is_array($form->getViewData()) ? implode('", "', array_keys($unknownValues)) : \gettype($form->getViewData()));
200+
$messageTemplate = $userInvalidMessage ?? 'The value {{ value }} is not valid.';
197201

198202
if (null !== $this->translator) {
199203
$message = $this->translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString], 'validators');
200204
} else {
201205
$message = strtr($messageTemplate, ['{{ value }}' => $clientDataAsString]);
202206
}
203207

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

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,32 @@ public function testAdjustFullNameForMultipleNonExpanded()
18611861
$this->assertSame('name[]', $view->vars['full_name']);
18621862
}
18631863

1864+
public function testInvalidMessageAwarenessForMultiple()
1865+
{
1866+
$form = $this->factory->create(static::TESTED_TYPE, null, [
1867+
'multiple' => true,
1868+
'expanded' => false,
1869+
'choices' => $this->choices,
1870+
'invalid_message' => 'You are not able to use value "{{ value }}"',
1871+
]);
1872+
1873+
$form->submit(['My invalid choice']);
1874+
$this->assertEquals("ERROR: You are not able to use value \"My invalid choice\"\n", (string) $form->getErrors(true));
1875+
}
1876+
1877+
public function testInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData()
1878+
{
1879+
$form = $this->factory->create(static::TESTED_TYPE, null, [
1880+
'multiple' => true,
1881+
'expanded' => false,
1882+
'choices' => $this->choices,
1883+
'invalid_message' => 'You are not able to use value "{{ value }}"',
1884+
]);
1885+
1886+
$form->submit(new \stdClass());
1887+
$this->assertEquals("ERROR: You are not able to use value \"stdClass\"\n", (string) $form->getErrors(true));
1888+
}
1889+
18641890
// https://github.com/symfony/symfony/issues/3298
18651891
public function testInitializeWithEmptyChoices()
18661892
{

0 commit comments

Comments
 (0)
0