[Form] Fixed: ChoiceType omits the "empty_value" option if the choices contain an empty element
This commit is contained in:
parent
7f9fd11fd0
commit
040ba8f3cd
@ -141,3 +141,4 @@ CHANGELOG
|
|||||||
* FormBuilder now implements \IteratorAggregate
|
* FormBuilder now implements \IteratorAggregate
|
||||||
* [BC BREAK] compound forms now always need a data mapper
|
* [BC BREAK] compound forms now always need a data mapper
|
||||||
* FormBuilder now maintains the order when explicitely adding form builders as children
|
* 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
|
||||||
|
@ -81,9 +81,15 @@ class ChoiceType extends AbstractType
|
|||||||
'preferred_choices' => $options['choice_list']->getPreferredViews(),
|
'preferred_choices' => $options['choice_list']->getPreferredViews(),
|
||||||
'choices' => $options['choice_list']->getRemainingViews(),
|
'choices' => $options['choice_list']->getRemainingViews(),
|
||||||
'separator' => '-------------------',
|
'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']) {
|
if ($options['multiple'] && !$options['expanded']) {
|
||||||
// Add "[]" to the name in case a select tag with multiple options is
|
// 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
|
// displayed. Otherwise only one of the selected options is sent in the
|
||||||
|
@ -613,6 +613,23 @@ class ChoiceTypeTest extends TypeTestCase
|
|||||||
$this->assertEquals($viewValue, $view->getVar('empty_value'));
|
$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()
|
public function getOptionsWithEmptyValue()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
Reference in New Issue
Block a user