Skip to content

Commit

Permalink
merged branch bschussek/issue3864 (PR #4801)
Browse files Browse the repository at this point in the history
Commits
-------

040ba8f [Form] Fixed: ChoiceType omits the "empty_value" option if the choices contain an empty element

Discussion
----------

[Form] Fixed: ChoiceType omits the "empty_value" option if the choices contain an empty element

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #3854, #3864
Todo: -
  • Loading branch information
fabpot committed Jul 9, 2012
2 parents 8680571 + 040ba8f commit 38c30b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,4 @@ CHANGELOG
* FormBuilder now implements \IteratorAggregate
* [BC BREAK] compound forms now always need a data mapper
* FormBuilder now maintains the order when explicitely adding form builders as children
* ChoiceType now doesn't add the empty value anymore if the choices already contain an empty element
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ public function buildView(FormViewInterface $view, FormInterface $form, array $o
'preferred_choices' => $options['choice_list']->getPreferredViews(),
'choices' => $options['choice_list']->getRemainingViews(),
'separator' => '-------------------',
'empty_value' => $options['empty_value'],
'empty_value' => null,
));

// Check if the choices already contain the empty value
// Only add the empty value option if this is not the case
if (0 === count($options['choice_list']->getIndicesForValues(array('')))) {
$view->setVar('empty_value', $options['empty_value']);
}

if ($options['multiple'] && !$options['expanded']) {
// Add "[]" to the name in case a select tag with multiple options is
// displayed. Otherwise only one of the selected options is sent in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,23 @@ public function testPassEmptyValueToView($multiple, $expanded, $required, $empty
$this->assertEquals($viewValue, $view->getVar('empty_value'));
}

/**
* @dataProvider getOptionsWithEmptyValue
*/
public function testDontPassEmptyValueIfContainedInChoices($multiple, $expanded, $required, $emptyValue, $viewValue)
{
$form = $this->factory->create('choice', null, array(
'multiple' => $multiple,
'expanded' => $expanded,
'required' => $required,
'empty_value' => $emptyValue,
'choices' => array('a' => 'A', '' => 'Empty'),
));
$view = $form->createView();

$this->assertNull($view->getVar('empty_value'));
}

public function getOptionsWithEmptyValue()
{
return array(
Expand Down

0 comments on commit 38c30b7

Please sign in to comment.