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

Skip to content

Commit caf7a3f

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[Form] Fix 'invalid_message' use in multiple ChoiceType
1 parent 6608880 commit caf7a3f

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
@@ -186,21 +186,25 @@ public function buildForm(FormBuilderInterface $builder, array $options)
186186
}
187187

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

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

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

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

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

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

1852+
public function testInvalidMessageAwarenessForMultiple()
1853+
{
1854+
$form = $this->factory->create(static::TESTED_TYPE, null, [
1855+
'multiple' => true,
1856+
'expanded' => false,
1857+
'choices' => $this->choices,
1858+
'invalid_message' => 'You are not able to use value "{{ value }}"',
1859+
]);
1860+
1861+
$form->submit(['My invalid choice']);
1862+
$this->assertEquals("ERROR: You are not able to use value \"My invalid choice\"\n", (string) $form->getErrors(true));
1863+
}
1864+
1865+
public function testInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData()
1866+
{
1867+
$form = $this->factory->create(static::TESTED_TYPE, null, [
1868+
'multiple' => true,
1869+
'expanded' => false,
1870+
'choices' => $this->choices,
1871+
'invalid_message' => 'You are not able to use value "{{ value }}"',
1872+
]);
1873+
1874+
$form->submit(new \stdClass());
1875+
$this->assertEquals("ERROR: You are not able to use value \"stdClass\"\n", (string) $form->getErrors(true));
1876+
}
1877+
18521878
// https://github.com/symfony/symfony/issues/3298
18531879
public function testInitializeWithEmptyChoices()
18541880
{

0 commit comments

Comments
 (0)
0