Navigation Menu

Skip to content

Commit

Permalink
[Form] made required part of the algorithm to determine if an empty v…
Browse files Browse the repository at this point in the history
…alue should be added to a choice
  • Loading branch information
fabpot committed Jun 23, 2011
1 parent a395582 commit 6de97c5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
19 changes: 17 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Expand Up @@ -65,13 +65,28 @@ public function buildForm(FormBuilder $builder, array $options)
}
}

// empty value
if ($options['multiple'] || $options['expanded']) {
// never use and empty value for these cases
$emptyValue = null;
} elseif (false === $options['empty_value']) {
// an empty value should be added but the user decided otherwise
$options['empty_value'] = null;
} elseif (null === $options['empty_value']) {
// user did not made a decision, so we put a blank empty value
$emptyValue = $options['required'] ? null : '';
} else {
// empty value has been set explicitely
$emptyValue = $options['empty_value'];
}

$builder
->setAttribute('choice_list', $options['choice_list'])
->setAttribute('preferred_choices', $options['preferred_choices'])
->setAttribute('multiple', $options['multiple'])
->setAttribute('expanded', $options['expanded'])
->setAttribute('required', $options['required'])
->setAttribute('empty_value', $options['multiple'] || $options['expanded'] ? null : $options['empty_value'])
->setAttribute('empty_value', $emptyValue)
;

if ($options['expanded']) {
Expand Down Expand Up @@ -133,7 +148,7 @@ public function getDefaultOptions(array $options)
'choices' => array(),
'preferred_choices' => array(),
'empty_data' => $multiple || $expanded ? array() : '',
'empty_value' => ($multiple || $expanded) || !isset($options['empty_value']) ? null : '',
'empty_value' => $multiple || $expanded || !isset($options['empty_value']) ? null : '',
'error_bubbling' => false,
);
}
Expand Down
Expand Up @@ -33,13 +33,15 @@ public function buildForm(FormBuilder $builder, array $options)
'months',
'days',
'empty_value',
'required',
)));
$timeOptions = array_intersect_key($options, array_flip(array(
'hours',
'minutes',
'seconds',
'with_seconds',
'empty_value',
'required',
)));

if (isset($options['date_widget'])) {
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -55,18 +55,21 @@ public function buildForm(FormBuilder $builder, array $options)
array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT
),
'empty_value' => $options['empty_value']['year'],
'required' => $options['required'],
);
$monthOptions = array(
'choice_list' => new MonthChoiceList(
$formatter, $options['months']
),
'empty_value' => $options['empty_value']['month'],
'required' => $options['required'],
);
$dayOptions = array(
'choice_list' => new PaddedChoiceList(
array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT
),
'empty_value' => $options['empty_value']['day'],
'required' => $options['required'],
);
}

Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -40,13 +40,15 @@ public function buildForm(FormBuilder $builder, array $options)
'choice_list' => new PaddedChoiceList(
array_combine($options['hours'], $options['hours']), 2, '0', STR_PAD_LEFT
),
'empty_value' => $options['empty_value']['hour']
'empty_value' => $options['empty_value']['hour'],
'required' => $options['required'],
))
->add('minute', $options['widget'], array(
'choice_list' => new PaddedChoiceList(
array_combine($options['minutes'], $options['minutes']), 2, '0', STR_PAD_LEFT
),
'empty_value' => $options['empty_value']['minute']
'empty_value' => $options['empty_value']['minute'],
'required' => $options['required'],
))
;

Expand All @@ -55,7 +57,8 @@ public function buildForm(FormBuilder $builder, array $options)
'choice_list' => new PaddedChoiceList(
array_combine($options['seconds'], $options['seconds']), 2, '0', STR_PAD_LEFT
),
'empty_value' => $options['empty_value']['second']
'empty_value' => $options['empty_value']['second'],
'required' => $options['required'],
));
}
}
Expand Down
10 changes: 6 additions & 4 deletions tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php
Expand Up @@ -381,10 +381,11 @@ public function testSingleChoiceNonRequired()
[@name="na&me"]
[not(@required)]
[
./option[@value="&a"][@selected="selected"][.="Choice&A"]
./option[@value=""][.="[trans][/trans]"]
/following-sibling::option[@value="&a"][@selected="selected"][.="Choice&A"]
/following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
]
[count(./option)=2]
[count(./option)=3]
'
);
}
Expand All @@ -404,10 +405,11 @@ public function testSingleChoiceNonRequiredNoneSelected()
[@name="na&me"]
[not(@required)]
[
./option[@value="&a"][not(@selected)][.="Choice&A"]
./option[@value=""][.="[trans][/trans]"]
/following-sibling::option[@value="&a"][not(@selected)][.="Choice&A"]
/following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
]
[count(./option)=2]
[count(./option)=3]
'
);
}
Expand Down

0 comments on commit 6de97c5

Please sign in to comment.