8000 bug #17798 [Form] allow `choice_label` option to be `false` · symfony/symfony@017e1d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 017e1d9

Browse files
committed
bug #17798 [Form] allow choice_label option to be false
When `ChoiceType`is expanded, `choice_label` option can be `false` or return `false` for one or more choices by a callable.
1 parent 2238398 commit 017e1d9

File tree

4 files changed

+419
-3
lines changed

4 files changed

+419
-3
lines changed

src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,21 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
155155
$key = $keys[$value];
156156
$nextIndex = is_int($index) ? $index++ : call_user_func($index, $choice, $key, $value);
157157

158+
// BC normalize label to accept a false value
159+
if (null === $< 10000 /span>label) {
160+
// If the labels are null, use the original choice key by default
161+
$label = (string) $key;
162+
} elseif (false !== $label) {
163+
// If "choice_label" is set to false and "expanded" is true, the value false
164+
// should be passed on to the "label" option of the checkboxes/radio buttons
165+
$dynamicLabel = call_user_func($label, $choice, $key, $value);
166+
$label = false === $dynamicLabel ? false : (string) $dynamicLabel;
167+
}
168+
158169
$view = new ChoiceView(
159170
$choice,
160171
$value,
161-
// If the labels are null, use the original choice key by default
162-
null === $label ? (string) $key : (string) call_user_func($label, $choice, $key, $value),
172+
$label,
163173
// The attributes may be a callable or a mapping from choice indices
164174
// to nested arrays
165175
is_callable($attr) ? call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public function configureOptions(OptionsResolver $resolver)
413413
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
414414
$resolver->setAllowedTypes('choices_as_values', 'bool');
415415
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
416-
$resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
416+
$resolver->setAllowedTypes('choice_label', array('null', 'bool', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
417417
$resolver->setAllowedTypes('choice_name', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
418418
$resolver->setAllowedTypes('choice_value', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
419419
$resolver->setAllowedTypes('choice_attr', array('null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));

src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,129 @@ public function testSingleChoiceExpanded()
690690
);
691691
}
692692

693+
public function testSingleChoiceExpandedWithLabelsAsFalse()
694+
{
695+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
696+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
697+
'choices_as_values' => true,
698+
'choice_label' => false,
699+
'multiple' => false,
700+
'expanded' => true,
701+
));
702+
703+
$this->assertWidgetMatchesXpath($form->createView(), array(),
704+
'/div
705+
[
706+
./div
707+
[@class="radio"]
708+
[
709+
./label
710+
[
711+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
712+
]
713+
]
714+
/following-sibling::div
715+
[@class="radio"]
716+
[
717+
./label
718+
[
719+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
720+
]
721+
]
722+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
723+
]
724+
'
725+
);
726+
}
727+
728+
public function testSingleChoiceExpandedWithLabelsSetByCallable()
729+
{
730+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
731+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'),
732+
'choices_as_values' => true,
733+
'choice_label' => function ($choice, $label, $value) {
734+
if ('&b' === $choice) {
735+
return false;
736+
}
737+
738+
return 'label.'.$value;
739+
},
740+
'multiple' => false,
741+
'expanded' => true,
742+
));
743+
744+
$this->assertWidgetMatchesXpath($form->createView(), array(),
745+
'/div
746+
[
747+
./div
748+
[@class="radio"]
749+
[
750+
./label
751+
[.=" [trans]label.&a[/trans]"]
752+
[
753+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
754+
]
755+
]
756+
/following-sibling::div
757+
[@class="radio"]
758+
[
759+
./label
760+
[
761+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
762+
]
763+
]
764+
/following-sibling::div
765+
[@class="radio"]
766+
[
767+
./label
768+
[.=" [trans]label.&c[/trans]"]
769+
[
770+
./input[@type="radio"][@name="name"][@id="name_2"][@value="&c"][not(@checked)]
771+
]
772+
]
773+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
774+
]
775+
'
776+
);
777+
}
778+
779+
public function testSingleChoiceExpandedWithLabelsSetFalseByCallable()
780+
{
781+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
782+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
783+
'choices_as_values' => true,
784+
'choice_label' => function () {
785+
return false;
786+
},
787+
'multiple' => false,
788+
'expanded' => true,
789+
));
790+
791+
$this->assertWidgetMatchesXpath($form->createView(), array(),
792+
'/div
793+
[
794+
./div
795+
[@class="radio"]
796+
[
797+
./label
798+
[
799+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
800+
]
801+
]
802+
/following-sibling::div
803+
[@class="radio"]
804+
[
805+
./label
806+
[
807+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
808+
]
809+
]
810+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
811+
]
812+
'
813+
);
814+
}
815+
693816
public function testSingleChoiceExpandedWithoutTranslation()
694817
{
695818
$form = $this->factory->createNamed('name', 'choice', '&a', array(
@@ -895,6 +1018,129 @@ public function testMultipleChoiceExpanded()
8951018
);
8961019
}
8971020

1021+
public function testMultipleChoiceExpandedWithLabelsAsFalse()
1022+
{
1023+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1024+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
1025+
'choices_as_values' => true,
1026+
'choice_label' => false,
1027+
'multiple' => true,
1028+
'expanded' => true,
1029+
));
1030+
1031+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1032+
'/div
1033+
[
1034+
./div
1035+
[@class="checkbox"]
1036+
[
1037+
./label
1038+
[
1039+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1040+
]
1041+
]
1042+
/following-sibling::div
1043+
[@class="checkbox"]
1044+
[
1045+
./label
1046+
[
1047+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1048+
]
1049+
]
1050+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1051+
]
1052+
'
1053+
);
1054+
}
1055+
1056+
public function testMultipleChoiceExpandedWithLabelsSetByCallable()
1057+
{
1058+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1059+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'),
1060+
'choices_as_values' => true,
1061+
'choice_label' => function ($choice, $label, $value) {
1062+
if ('&b' === $choice) {
1063+
return false;
1064+
}
1065+
1066+
return 'label.'.$value;
1067+
},
1068+
'multiple' => true,
1069+
'expanded' => true,
1070+
));
1071+
1072+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1073+
'/div
1074+
[
1075+
./div
1076+
[@class="checkbox"]
1077+
[
1078+
./label
1079+
[.=" [trans]label.&a[/trans]"]
1080+
[
1081+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1082+
]
1083+
]
1084+
/following-sibling::div
1085+
[@class="checkbox"]
1086+
[
1087+
./label
1088+
[
1089+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1090+
]
1091+
]
1092+
/following-sibling::div
1093+
[@class="checkbox"]
1094+
[
1095+
./label
1096+
[.=" [trans]label.&c[/trans]"]
1097+
[
1098+
./input[@type="checkbox"][@name="name[]"][@id="name_2"][@value="&c"][not(@checked)]
1099+
]
1100+
]
1101+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1102+
]
1103+
'
1104+
);
1105+
}
1106+
1107+
public function testMultipleChoiceExpandedWithLabelsSetFalseByCallable()
1108+
{
1109+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1110+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
1111+
'choices_as_values' => true,
1112+
'choice_label' => function () {
1113+
return false;
1114+
},
1115+
'multiple' => true,
1116+
'expanded' => true,
1117+
));
1118+
1119+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1120+
'/div
1121+
[
1122+
./div
1123+
[@class="checkbox"]
1124+
[
1125+
./label
1126+
[
1127+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1128+
]
1129+
]
1130+
/following-sibling::div
1131+
[@class="checkbox"]
1132+
[
1133+
./label
1134+
[
1135+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1136+
]
1137+
]
1138+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1139+
]
1140+
'
1141+
);
1142+
}
1143+
8981144
public function testMultipleChoiceExpandedWithoutTranslation()
8991145
{
9001146
$form = $this->factory->createNamed('name', 'choice', array('&a', '&c'), array(

0 commit comments

Comments
 (0)
0