Skip to content

Commit 6de97c5

Browse files
committed
[Form] made required part of the algorithm to determine if an empty value should be added to a choice
1 parent a395582 commit 6de97c5

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,28 @@ public function buildForm(FormBuilder $builder, array $options)
6565
}
6666
}
6767

68+
// empty value
69+
if ($options['multiple'] || $options['expanded']) {
70+
// never use and empty value for these cases
71+
$emptyValue = null;
72+
} elseif (false === $options['empty_value']) {
73+
// an empty value should be added but the user decided otherwise
74+
$options['empty_value'] = null;
75+
} elseif (null === $options['empty_value']) {
76+
// user did not made a decision, so we put a blank empty value
77+
$emptyValue = $options['required'] ? null : '';
78+
} else {
79+
// empty value has been set explicitely
80+
$emptyValue = $options['empty_value'];
81+
}
82+
6883
$builder
6984
->setAttribute('choice_list', $options['choice_list'])
7085
->setAttribute('preferred_choices', $options['preferred_choices'])
7186
->setAttribute('multiple', $options['multiple'])
7287
->setAttribute('expanded', $options['expanded'])
7388
->setAttribute('required', $options['required'])
74-
->setAttribute('empty_value', $options['multiple'] || $options['expanded'] ? null : $options['empty_value'])
89+
->setAttribute('empty_value', $emptyValue)
7590
;
7691

7792
if ($options['expanded']) {
@@ -133,7 +148,7 @@ public function getDefaultOptions(array $options)
133148
'choices' => array(),
134149
'preferred_choices' => array(),
135150
'empty_data' => $multiple || $expanded ? array() : '',
136-
'empty_value' => ($multiple || $expanded) || !isset($options['empty_value']) ? null : '',
151+
'empty_value' => $multiple || $expanded || !isset($options['empty_value']) ? null : '',
137152
'error_bubbling' => false,
138153
);
139154
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ public function buildForm(FormBuilder $builder, array $options)
3333
'months',
3434
'days',
3535
'empty_value',
36+
'required',
3637
)));
3738
$timeOptions = array_intersect_key($options, array_flip(array(
3839
'hours',
3940
'minutes',
4041
'seconds',
4142
'with_seconds',
4243
'empty_value',
44+
'required',
4345
)));
4446

4547
if (isset($options['date_widget'])) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,21 @@ public function buildForm(FormBuilder $builder, array $options)
5555
array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT
5656
),
5757
'empty_value' => $options['empty_value']['year'],
58+
'required' => $options['required'],
5859
);
5960
$monthOptions = array(
6061
'choice_list' => new MonthChoiceList(
6162
$formatter, $options['months']
6263
),
6364
'empty_value' => $options['empty_value']['month'],
65+
'required' => $options['required'],
6466
);
6567
$dayOptions = array(
6668
'choice_list' => new PaddedChoiceList(
6769
array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT
6870
),
6971
'empty_value' => $options['empty_value']['day'],
72+
'required' => $options['required'],
7073
);
7174
}
7275

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ public function buildForm(FormBuilder $builder, array $options)
4040
'choice_list' => new PaddedChoiceList(
4141
array_combine($options['hours'], $options['hours']), 2, '0', STR_PAD_LEFT
4242
),
43-
'empty_value' => $options['empty_value']['hour']
43+
'empty_value' => $options['empty_value']['hour'],
44+
'required' => $options['required'],
4445
))
4546
->add('minute', $options['widget'], array(
4647
'choice_list' => new PaddedChoiceList(
4748
array_combine($options['minutes'], $options['minutes']), 2, '0', STR_PAD_LEFT
4849
),
49-
'empty_value' => $options['empty_value']['minute']
50+
'empty_value' => $options['empty_value']['minute'],
51+
'required' => $options['required'],
5052
))
5153
;
5254

@@ -55,7 +57,8 @@ public function buildForm(FormBuilder $builder, array $options)
5557
'choice_list' => new PaddedChoiceList(
5658
array_combine($options['seconds'], $options['seconds']), 2, '0', STR_PAD_LEFT
5759
),
58-
'empty_value' => $options['empty_value']['second']
60+
'empty_value' => $options['empty_value']['second'],
61+
'required' => $options['required'],
5962
));
6063
}
6164
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,11 @@ public function testSingleChoiceNonRequired()
381381
[@name="na&me"]
382382
[not(@required)]
383383
[
384-
./option[@value="&a"][@selected="selected"][.="Choice&A"]
384+
./option[@value=""][.="[trans][/trans]"]
385+
/following-sibling::option[@value="&a"][@selected="selected"][.="Choice&A"]
385386
/following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
386387
]
387-
[count(./option)=2]
388+
[count(./option)=3]
388389
'
389390
);
390391
}
@@ -404,10 +405,11 @@ public function testSingleChoiceNonRequiredNoneSelected()
404405
[@name="na&me"]
405406
[not(@required)]
406407
[
407-
./option[@value="&a"][not(@selected)][.="Choice&A"]
408+
./option[@value=""][.="[trans][/trans]"]
409+
/following-sibling::option[@value="&a"][not(@selected)][.="Choice&A"]
408410
/following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
409411
]
410-
[count(./option)=2]
412+
[count(./option)=3]
411413
'
412414
);
413415
}

0 commit comments

Comments
 (0)