Skip to content

Commit b5fddcb

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: [HttpClient][PHPDoc] Fix 2 remaining return mixed [Cache] [FrameworkBundle] Fix logging for TagAwareAdapter [Route] Better inline requirements and defaults parsing Simplified condition and removed unused code from AbstractSessionListener::onKernelRequest [PhpUnitBridge] Fix phpunit symlink on Windows [Yaml] Fixed infinite loop when parser goes through an additional and invalid closing tag [Form] Fix 'invalid_message' use in multiple ChoiceType
2 parents cf1afee + e82ddf2 commit b5fddcb

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
@@ -187,21 +187,22 @@ 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+
$messageTemplate = $options['invalid_message'] ?? 'The value {{ value }} is not valid.';
191+
192+
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues, $messageTemplate) {
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()) ? (string) $form->getViewData() : (\is_array($form->getViewData()) ? implode('", "', array_keys($unknownValues)) : \gettype($form->getViewData()));
197198

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

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

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)