E5E7 [Form] Fix 'invalid_message' use in multiple ChoiceType by alexandre-daubois · Pull Request #40660 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

if ($options['multiple']) {
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues) {
$messageTemplate = $options['invalid_message'] ?? 'The value {{ value }} is not valid.';

$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues, $messageTemplate) {
// Throw exception if unknown values were submitted
if (\count($unknownValues) > 0) {
$form = $event->getForm();

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,32 @@ public function testAdjustFullNameForMultipleNonExpanded()
$this->assertSame('name[]', $view->vars['full_name']);
}

public function testInvalidMessageAwarenessForMultiple()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'multiple' => true,
'expanded' => false,
'choices' => $this->choices,
'invalid_message' => 'You are not able to use value "{{ value }}"',
]);

$form->submit(['My invalid choice']);
$this->assertEquals("ERROR: You are not able to use value \"My invalid choice\"\n", (string) $form->getErrors(true));
}

public function testInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'multiple' => true,
'expanded' => false,
'choices' => $this->choices,
'invalid_message' => 'You are not able to use value "{{ value }}"',
]);

$form->submit(new \stdClass());
$this->assertEquals("ERROR: You are not able to use value \"stdClass\"\n", (string) $form->getErrors(true));
}

// https://github.com/symfony/symfony/issues/3298
public function testInitializeWithEmptyChoices()
{
Expand Down
0