Skip to content

Commit e82ddf2

Browse files
committed
bug #40660 [Form] Fix 'invalid_message' use in multiple ChoiceType (alexandre-daubois)
This PR was merged into the 5.2 branch. Discussion ---------- [Form] Fix 'invalid_message' use in multiple ChoiceType | Q | A | ------------- | --- | Branch? | 5.2<!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #40636 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry --> `invalid_message` option were not take into account anymore since v5.2.4. This PR intends to fix this. The option `invalid_message` is now passed to the `POST_SUBMIT` callback, for multiple ChoiceType. Commits ------- f2516840c8 [Form] Fix 'invalid_message' use in multiple ChoiceType
2 parents e4130f1 + 2a817bf commit e82ddf2

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Extension/Core/Type/ChoiceType.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,22 @@ 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+
$messageTemplate = $options['invalid_message'] ?? 'The value {{ value }} is not valid.';
190+
191+
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues, $messageTemplate) {
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()) ? (string) $form->getViewData() : (\is_array($form->getViewData()) ? implode('", "', array_keys($unknownValues)) : \gettype($form->getViewData()));
196197

197198
if (null !== $this->translator) {
198199
$message = $this->translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString], 'validators');
199200
} else {
200201
$message = strtr($messageTemplate, ['{{ value }}' => $clientDataAsString]);
201202
}
202203

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

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)