8000 Merge branch '2.7' into 2.8 · symfony/symfony@82eb951 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 82eb951

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: bug #17798 [Form] allow `choice_label` option to be `false`
2 parents cc9f6b0 + b630ac7 commit 82eb951

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 === $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
@@ -422,7 +422,7 @@ public function configureOptions(OptionsResolver $resolver)
422422
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
423423
$resolver->setAllowedTypes('choices_as_values', 'bool');
424424
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
425-
$resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
425+
$resolver->setAllowedTypes('choice_label', array('null', 'bool', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
426426
$resolver->setAllowedTypes('choice_name', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
427427
$resolver->setAllowedTypes('choice_value', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
428428
$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
@@ -717,6 +717,129 @@ public function testSingleChoiceExpanded()
717717
);
718718
}
719719

720+
public function testSingleChoiceExpandedWithLabelsAsFalse()
721+
{
722+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
723+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
724+
'choices_as_values' => true,
725+
'choice_label' => false,
726+
'multiple' => false,
727+
'expanded' => true,
728+
));
729+
730+
$this->assertWidgetMatchesXpath($form->createView(), array(),
731+
'/div
732+
[
733+
./div
734+
[@class="radio"]
735+
[
736+
./label
737+
[
738+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
739+
]
740+
]
741+
/following-sibling::div
742+
[@class="radio"]
743+
[
744+
./label
745+
[
746+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
747+
]
748+
]
749+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
750+
]
751+
'
752+
);
753+
}
754+
755+
public function testSingleChoiceExpandedWithLabelsSetByCallable()
756+
{
757+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
758+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'),
759+
'choices_as_values' => true,
760+
'choice_label' => function ($choice, $label, $value) {
761+
if ('&b' === $choice) {
762+
return false;
763+
}
764+
765+
return 'label.'.$value;
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+
[.=" [trans]label.&a[/trans]"]
779+
[
780+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
781+
]
782+
]
783+
/following-sibling::div
784+
[@class="radio"]
785+
[
786+
./label
787+
[
788+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
789+
]
790+
]
791+
/following-sibling::div
792+
[@class="radio"]
793+
[
794+
./label
795+
[.=" [trans]label.&c[/trans]"]
796+
[
797+
./input[@type="radio"][@name="name"][@id="name_2"][@value="&c"][not(@checked)]
798+
]
799+
]
800+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
801+
]
802+
'
803+
);
804+
}
805+
806+
public function testSingleChoiceExpandedWithLabelsSetFalseByCallable()
807+
{
808+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
809+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
810+
'choices_as_values' => true,
811+
'choice_label' => function () {
812+
return false;
813+
},
814+
'multiple' => false,
815+
'expanded' => true,
816+
));
817+
818+
$this->assertWidgetMatchesXpath($form->createView(), array(),
819+
'/div
820+
[
821+
./div
822+
[@class="radio"]
823+
[
824+
./label
825+
[
826+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
827+
]
828+
]
829+
/following-sibling::div
830+
[@class="radio"]
831+
[
832+
./label
833+
[
834+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
835+
]
836+
]
837+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
838+
]
839+
'
840+
);
841+
}
842+
720843
public function testSingleChoiceExpandedWithoutTranslation()
721844
{
722845
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
@@ -970,6 +1093,129 @@ public function testMultipleChoiceExpanded()
9701093
);
9711094
}
9721095

1096+
public function testMultipleChoiceExpandedWithLabelsAsFalse()
1097+
{
1098+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1099+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
1100+
'choices_as_values' => true,
1101+
'choice_label' => false,
1102+
'multiple' => true,
1103+
'expanded' => true,
1104+
));
1105+
1106+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1107+
'/div
1108+
[
1109+
./div
1110+
[@class="checkbox"]
1111+
[
1112+
./label
1113+
[
1114+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1115+
]
1116+
]
1117+
/following-sibling::div
1118+
[@class="checkbox"]
1119+
[
1120+
./label
1121+
[
1122+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1123+
]
1124+
]
1125+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1126+
]
1127+
'
1128+
);
1129+
}
1130+
1131+
public function testMultipleChoiceExpandedWithLabelsSetByCallable()
1132+
{
1133+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1134+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'),
1135+
'choices_as_values' => true,
1136+
'choice_label' => function ($choice, $label, $value) {
1137+
if ('&b' === $choice) {
1138+
return false;
1139+
}
1140+
1141+
return 'label.'.$value;
1142+
},
1143+
'multiple' => true,
1144+
'expanded' => true,
1145+
));
1146+
1147+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1148+
'/div
1149+
[
1150+
./div
1151+
[@class="checkbox"]
1152+
[
1153+
./label
1154+
[.=" [trans]label.&a[/trans]"]
1155+
[
1156+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1157+
]
1158+
]
1159+
/following-sibling::div
1160+
[@class="checkbox"]
1161+
[
1162+
./label
1163+
[
1164+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1165+
]
1166+
]
1167+
/following-sibling::div
1168+
[@class="checkbox"]
1169+
[
1170+
./label
1171+
[.=" [trans]label.&c[/trans]"]
1172+
[
1173+
./input[@type="checkbox"][@name="name[]"][@id="name_2"][@value="&c"][not(@checked)]
1174+
]
1175+
]
1176+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1177+
]
1178+
'
1179+
);
1180+
}
1181+
1182+
public function testMultipleChoiceExpandedWithLabelsSetFalseByCallable()
1183+
{
1184+
$form = $this->factory->createNamed('name', 'choice', array('&a'), array(
1185+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
1186+
'choices_as_values' => true,
1187+
'choice_label' => function () {
1188+< F6D8 div class="diff-text-inner"> return false;
1189+
},
1190+
'multiple' => true,
1191+
'expanded' => true,
1192+
));
1193+
1194+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1195+
'/div
1196+
[
1197+
./div
1198+
[@class="checkbox"]
1199+
[
1200+
./label
1201+
[
1202+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1203+
]
1204+
]
1205+
/following-sibling::div
1206+
[@class="checkbox"]
1207+
[
1208+
./label
1209+
[
1210+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1211+
]
1212+
]
1213+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1214+
]
1215+
'
1216+
);
1217+
}
1218+
9731219
public function testMultipleChoiceExpandedWithoutTranslation()
9741220
{
9751221
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a', '&c'), array(

0 commit comments

Comments
 (0)
0