8000 allow `choice_label` option to be `false` · symfony/symfony@ab79ad5 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab79ad5

Browse files
committed
allow choice_label option to be false
1 parent dc59e42 commit ab79ad5

File tree

4 files changed

+417
-3
lines changed

4 files changed

+417
-3
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,19 @@ 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 === $label) {
160+
// If the labels are null, use the original choice key by default
161+
$label = (string) $key;
162+
} elseif (false !== $label) {
163+
$generatedLabel = call_user_func($label, $choice, $key, $value);
164+
$label = false === $generatedLabel ? false : $generatedLabel;
165+
}
166+
158167
$view = new ChoiceView(
159168
$choice,
160169
$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),
170+
$label,
163171
// The attributes may be a callable or a mapping from choice indices
164172
// to nested arrays
165173
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
@@ -402,7 +402,7 @@ public function configureOptions(OptionsResolver $resolver)
402402
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
403403
$resolver->setAllowedTypes('choices_as_values', 'bool');
404404
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
405-
$resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
405+
$resolver->setAllowedTypes('choice_label', array('null', 'bool', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
406406
$resolver->setAllowedTypes('choice_name', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
407407
$resolver->setAllowedTypes('choice_value', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
408408
$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
@@ -670,6 +670,129 @@ public function testSingleChoiceExpanded()
670670
);
671671
}
672672

673+
public function testSingleChoiceExpandedWithLabelsAsFalse()
674+
{
675+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
676+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
677+
'choices_as_values' => true,
678+
'choice_label' => false,
679+
'multiple' => false,
680+
'expanded' => true,
681+
));
682+
683+
$this->assertWidgetMatchesXpath($form->createView(), array(),
684+
'/div
685+
[
686+
./div
687+
[@class="radio"]
688+
[
689+
./label
690+
[
691+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
692+
]
693+
]
694+
/following-sibling::div
695+
[@class="radio"]
696+
[
697+
./label
698+
[
699+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
700+
]
701+
]
702+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
703+
]
704+
'
705+
);
706+
}
707+
708+
public function testSingleChoiceExpandedWithLabelsSetByCallable()
709+
{
710+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
711+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'),
712+
'choices_as_values' => true,
713+
'choice_label' => function ($choice, $label, $value) {
714+
if ('&b' === $choice) {
715+
return false;
716+
}
717+
718+
return 'label.'.$value;
719+
},
720+
'multiple' => false,
721+
'expanded' => true,
722+
));
723+
724+
$this->assertWidgetMatchesXpath($form->createView(), array(),
725+
'/div
726+
[
727+
./div
728+
[@class="radio"]
729+
[
730+
./label
731+
[.=" [trans]label.&a[/trans]"]
732+
[
733+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
734+
]
735+
]
736+
/following-sibling::div
737+
[@class="radio"]
738+
[
739+
./label
740+
[
741+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
742+
]
743+
]
744+
/following-sibling::div
745+
[@class="radio"]
746+
[
747+
./label
748+
[.=" [trans]label.&c[/trans]"]
749+
[
750+
./input[@type="radio"][@name="name"][@id="name_2"][@value="&c"][not(@checked)]
751+
]
752+
]
753+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
754+
]
755+
'
756+
);
757+
}
758+
759+
public function testSingleChoiceExpandedWithLabelsSetFalseByCallable()
760+
{
761+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
762+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
763+
'choices_as_values' => true,
764+
'choice_label' => function () {
765+
return false;
766+
},
767+
'multiple' => false,
768+
'expanded' => true,
769+
));
770+
771+
$this->assertWidgetMatchesXpath($form->createView(), array(),
772+
'/div
773+
[
774+
./div
775+
[@class="radio"]
776+
[
777+
./label
778+
[
779+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
780+
]
781+
]
782+
/following-sibling::div
783+
[@class="radio"]
784+
[
785+
./label
786+
[
787+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
788+
]
789+
]
790+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
791+
]
792+
'
793+
);
794+
}
795+
673796
public function testSingleChoiceExpandedWithoutTranslation()
674797
{
675798
$form = $this->factory->createNamed('name', 'choice', '&a', array(
@@ -874,6 +997,129 @@ public function testMultipleChoiceExpanded()
874997
);
875998
}
876999

1000+
public function testMultipleChoiceExpandedWithLabelsAsFalse()
1001+
{
1002+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1003+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
1004+
'choices_as_values' => true,
1005+
'choice_label' => false,
1006+
'multiple' => true,
1007+
'expanded' => true,
1008+
));
1009+
1010+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1011+
'/div
1012+
[
1013+
./div
1014+
[@class="checkbox"]
1015+
[
1016+
./label
1017+
[
1018+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1019+
]
1020+
]
1021+
/following-sibling::div
1022+
[@class="checkbox"]
1023+
[
1024+
./label
1025+
[
1026+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1027+
]
1028+
]
1029+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1030+
]
1031+
'
1032+
);
1033+
}
1034+
1035+
public function testMultipleChoiceExpandedWithLabelsSetByCallable()
1036+
{
1037+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1038+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'),
1039+
'choices_as_values' => true,
1040+
'choice_label' => function ($choice, $label, $value) {
1041+
if ('&b' === $choice) {
1042+
return false;
1043+
}
1044+
1045+
return 'label.'.$value;
1046+
},
1047+
'multiple' => true,
1048+
'expanded' => true,
1049+
));
1050+
1051+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1052+
'/div
1053+
[
1054+
./div
1055+
[@class="checkbox"]
1056+
[
1057+
./label
1058+
[.=" [trans]label.&a[/trans]"]
1059+
[
1060+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1061+
]
1062+
]
1063+
/following-sibling::div
1064+
[@class="checkbox"]
1065+
[
1066+
./label
1067+
[
1068+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1069+
]
1070+
]
1071+
/following-sibling::div
1072+
[@class="checkbox"]
1073+
[
1074+
./label
1075+
[.=" [trans]label.&c[/trans]"]
1076+
[
1077+
./input[@type="checkbox"][@name="name[]"][@id="name_2"][@value="&c"][not(@checked)]
1078+
]
1079+
]
1080+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1081+
]
1082+
'
1083+
);
1084+
}
1085+
1086+
public function testMultipleChoiceExpandedWithLabelsSetFalseByCallable()
1087+
{
1088+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1089+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
1090+
'choices_as_values' => true,
1091+
'choice_label' => function () {
1092+
return false;
1093+
},
1094+
'multiple' => true,
1095+
'expanded' => true,
1096+
));
1097+
1098+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1099+
'/div
1100+
[
1101+
./div
1102+
[@class="checkbox"]
1103+
[
1104+
./label
1105+
[
1106+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1107+
]
1108+
]
1109+
/following-sibling::div
1110+
[@class="checkbox"]
1111+
[
1112+
./label
1113+
[
1114+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1115+
]
1116+
]
1117+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1118+
]
1119+
'
1120+
);
1121+
}
1122+
8771123
public function testMultipleChoiceExpandedWithoutTranslation()
8781124
{
8791125
$form = $this->factory->createNamed('name', 'choice', array('&a', '&c'), array(

0 commit comments

Comments
 (0)
0