From 30c15f14fa4b8013ba0a8df5ed0b9cc02bd6f3cb Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Mon, 5 Aug 2019 17:10:59 +0200 Subject: [PATCH] [Form] use a choice label normalizer instead of violating \Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface::createView params --- .../ChoiceList/Factory/DefaultChoiceListFactory.php | 2 +- .../Component/Form/Extension/Core/Type/ChoiceType.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php index 45b56881a1c5b..9c440665b9764 100644 --- a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php +++ b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php @@ -124,7 +124,7 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $ if (null === $label) { // If the labels are null, use the original choice key by default $label = (string) $key; - } elseif (false !== $label) { + } else { // If "choice_label" is set to false and "expanded" is true, the value false // should be passed on to the "label" option of the checkboxes/radio buttons $dynamicLabel = \call_user_func($label, $choice, $key, $value); diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index 2ad0859e880fa..0e2ec721cf3f7 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -312,6 +312,16 @@ public function configureOptions(OptionsResolver $resolver) return $choiceTranslationDomain; }; + $choiceLabelNormalizer = function (Options $options, $choiceLabel) { + if (false === $choiceLabel) { + return function () { + return false; + }; + } + + return $choiceLabel; + }; + $resolver->setDefaults([ 'multiple' => false, 'expanded' => false, @@ -339,6 +349,7 @@ public function configureOptions(OptionsResolver $resolver) $resolver->setNormalizer('placeholder', $placeholderNormalizer); $resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer); $resolver->setNormalizer('choices_as_values', $choicesAsValuesNormalizer); + $resolver->setNormalizer('choice_label', $choiceLabelNormalizer); $resolver->setAllowedTypes('choices', ['null', 'array', '\Traversable']); $resolver->setAllowedTypes('choice_translation_domain', ['null', 'bool', 'string']);