Skip to content

Commit 4f1ae66

Browse files
committed
[Form] Fixed choice labels in expanded choice fields
1 parent 41c6ab0 commit 4f1ae66

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ public function buildForm(FormBuilder $builder, array $options)
4141

4242
foreach ($options['choices'] as $choice => $value) {
4343
if ($options['multiple']) {
44-
$builder->add((string)$choice, 'checkbox', array('value' => $choice));
44+
$builder->add((string)$choice, 'checkbox', array(
45+
'value' => $choice,
46+
'label' => $value,
47+
));
4548
} else {
46-
$builder->add((string)$choice, 'radio', array('value' => $choice));
49+
$builder->add((string)$choice, 'radio', array(
50+
'value' => $choice,
51+
'label' => $value,
52+
));
4753
}
4854
}
4955
}

tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function testSingleChoice()
236236
{
237237
$form = $this->factory->create('choice', 'na&me', array(
238238
'property_path' => 'name',
239-
'choices' => array('a' => 'A', 'b' => 'B'),
239+
'choices' => array('a' => 'Choice A', 'b' => 'Choice B'),
240240
'data' => 'a',
241241
'multiple' => false,
242242
'expanded' => false,
@@ -246,8 +246,8 @@ public function testSingleChoice()
246246
'/select
247247
[@name="na&me"]
248248
[
249-
./option[@value="a"][@selected="selected"][.="A"]
250-
/following-sibling::option[@value="b"][not(@selected)][.="B"]
249+
./option[@value="a"][@selected="selected"][.="Choice A"]
250+
/following-sibling::option[@value="b"][not(@selected)][.="Choice B"]
251251
]
252252
[count(./option)=2]
253253
'
@@ -258,7 +258,7 @@ public function testSingleChoiceWithPreferred()
258258
{
259259
$form = $this->factory->create('choice', 'na&me', array(
260260
'property_path' => 'name',
261-
'choices' => array('a' => 'A', 'b' => 'B'),
261+
'choices' => array('a' => 'Choice A', 'b' => 'Choice B'),
262262
'preferred_choices' => array('b'),
263263
'data' => 'a',
264264
'multiple' => false,
@@ -269,9 +269,9 @@ public function testSingleChoiceWithPreferred()
269269
'/select
270270
[@name="na&me"]
271271
[
272-
./option[@value="b"][not(@selected)][.="B"]
272+
./option[@value="b"][not(@selected)][.="Choice B"]
273273
/following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
274-
/following-sibling::option[@value="a"][@selected="selected"][.="A"]
274+
/following-sibling::option[@value="a"][@selected="selected"][.="Choice A"]
275275
]
276276
[count(./option)=3]
277277
'
@@ -282,7 +282,7 @@ public function testSingleChoiceNonRequired()
282282
{
283283
$form = $this->factory->create('choice', 'na&me', array(
284284
'property_path' => 'name',
285-
'choices' => array('a' => 'A', 'b' => 'B'),
285+
'choices' => array('a' => 'Choice A', 'b' => 'Choice B'),
286286
'required' => false,
287287
'data' => 'a',
288288
'multiple' => false,
@@ -294,8 +294,8 @@ public function testSingleChoiceNonRequired()
294294
[@name="na&me"]
295295
[
296296
./option[@value=""][.=""]
297-
/following-sibling::option[@value="a"][@selected="selected"][.="A"]
298-
/following-sibling::option[@value="b"][not(@selected)][.="B"]
297+
/following-sibling::option[@value="a"][@selected="selected"][.="Choice A"]
298+
/following-sibling::option[@value="b"][not(@selected)][.="Choice B"]
299299
]
300300
[count(./option)=3]
301301
'
@@ -307,8 +307,8 @@ public function testSingleChoiceGrouped()
307307
$form = $this->factory->create('choice', 'na&me', array(
308308
'property_path' => 'name',
309309
'choices' => array(
310-
'Group1' => array('a' => 'A', 'b' => 'B'),
311-
'Group2' => array('c' => 'C'),
310+
'Group1' => array('a' => 'Choice A', 'b' => 'Choice B'),
311+
'Group2' => array('c' => 'Choice C'),
312312
),
313313
'data' => 'a',
314314
'multiple' => false,
@@ -320,13 +320,13 @@ public function testSingleChoiceGrouped()
320320
[@name="na&me"]
321321
[./optgroup[@label="Group1"]
322322
[
323-
./option[@value="a"][@selected="selected"][.="A"]
324-
/following-sibling::option[@value="b"][not(@selected)][.="B"]
323+
./option[@value="a"][@selected="selected"][.="Choice A"]
324+
/following-sibling::option[@value="b"][not(@selected)][.="Choice B"]
325325
]
326326
[count(./option)=2]
327327
]
328328
[./optgroup[@label="Group2"]
329-
[./option[@value="c"][not(@selected)][.="C"]]
329+
[./option[@value="c"][not(@selected)][.="Choice C"]]
330330
[count(./option)=1]
331331
]
332332
[count(./optgroup)=2]
@@ -338,7 +338,7 @@ public function testMultipleChoice()
338338
{
339339
$form = $this->factory->create('choice', 'na&me', array(
340340
'property_path' => 'name',
341-
'choices' => array('a' => 'A', 'b' => 'B'),
341+
'choices' => array('a' => 'Choice A', 'b' => 'Choice B'),
342342
'data' => array('a'),
343343
'multiple' => true,
344344
'expanded' => false,
@@ -349,8 +349,8 @@ public function testMultipleChoice()
349349
[@name="na&me[]"]
350350
[@multiple="multiple"]
351351
[
352-
./option[@value="a"][@selected="selected"][.="A"]
353-
/following-sibling::option[@value="b"][not(@selected)][.="B"]
352+
./option[@value="a"][@selected="selected"][.="Choice A"]
353+
/following-sibling::option[@value="b"][not(@selected)][.="Choice B"]
354354
]
355355
[count(./option)=2]
356356
'
@@ -361,7 +361,7 @@ public function testMultipleChoiceNonRequired()
361361
{
362362
$form = $this->factory->create('choice', 'na&me', array(
363363
'property_path' => 'name',
364-
'choices' => array('a' => 'A', 'b' => 'B'),
364+
'choices' => array('a' => 'Choice A', 'b' => 'Choice B'),
365365
'data' => array('a'),
366366
'required' => false,
367367
'multiple' => true,
@@ -373,8 +373,8 @@ public function testMultipleChoiceNonRequired()
373373
[@name="na&me[]"]
374374
[@multiple="multiple"]
375375
[
376-
./option[@value="a"][@selected="selected"][.="A"]
377-
/following-sibling::option[@value="b"][not(@selected)][.="B"]
376+
./option[@value="a"][@selected="selected"][.="Choice A"]
377+
/following-sibling::option[@value="b"][not(@selected)][.="Choice B"]
378378
]
379379
[count(./option)=2]
380380
'
@@ -385,7 +385,7 @@ public function testSingleChoiceExpanded()
385385
{
386386
$form = $this->factory->create('choice', 'na&me', array(
387387
'property_path' => 'name',
388-
'choices' => array('a' => 'A', 'b' => 'B'),
388+
'choices' => array('a' => 'Choice A', 'b' => 'Choice B'),
389389
'data' => 'a',
390390
'multiple' => false,
391391
'expanded' => true,
@@ -395,9 +395,9 @@ public function testSingleChoiceExpanded()
395395
'/div
396396
[
397397
./input[@type="radio"][@name="na&me"][@id="na&me_a"][@checked]
398-
/following-sibling::label[@for="na&me_a"][.="[trans]A[/trans]"]
398+
/following-sibling::label[@for="na&me_a"][.="[trans]Choice A[/trans]"]
399399
/following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_b"][not(@checked)]
400-
/following-sibling::label[@for="na&me_b"][.="[trans]B[/trans]"]
400+
/following-sibling::label[@for="na&me_b"][.="[trans]Choice B[/trans]"]
401401
]
402402
[count(./input)=2]
403403
'
@@ -408,7 +408,7 @@ public function testMultipleChoiceExpanded()
408408
{
409409
$form = $this->factory->create('choice', 'na&me', array(
410410
'property_path' => 'name',
411-
'choices' => array('a' => 'A', 'b' => 'B', 'c' => 'C'),
411+
'choices' => array('a' => 'Choice A', 'b' => 'Choice B', 'c' => 'Choice C'),
412412
'data' => array('a', 'c'),
413413
'multiple' => true,
414414
'expanded' => true,
@@ -418,11 +418,11 @@ public function testMultipleChoiceExpanded()
418418
'/div
419419
[
420420
./input[@type="checkbox"][@name="na&me[a]"][@id="na&me_a"][@checked]
421-
/following-sibling::label[@for="na&me_a"][.="[trans]A[/trans]"]
421+
/following-sibling::label[@for="na&me_a"][.="[trans]Choice A[/trans]"]
422422
/following-sibling::input[@type="checkbox"][@name="na&me[b]"][@id="na&me_b"][not(@checked)]
423-
/following-sibling::label[@for="na&me_b"][.="[trans]B[/trans]"]
423+
/following-sibling::label[@for="na&me_b"][.="[trans]Choice B[/trans]"]
424424
/following-sibling::input[@type="checkbox"][@name="na&me[c]"][@id="na&me_c"][@checked]
425-
/following-sibling::label[@for="na&me_c"][.="[trans]C[/trans]"]
425+
/following-sibling::label[@for="na&me_c"][.="[trans]Choice C[/trans]"]
426426
]
427427
[count(./input)=3]
428428
'

0 commit comments

Comments
 (0)