[Form] Moved calculation of ChoiceType options to closures
This commit is contained in:
parent
5adec19f56
commit
8329087a20
@ -77,6 +77,7 @@ abstract class DoctrineType extends AbstractType
|
|||||||
'property' => null,
|
'property' => null,
|
||||||
'query_builder' => null,
|
'query_builder' => null,
|
||||||
'loader' => $loader,
|
'loader' => $loader,
|
||||||
|
'choices' => null,
|
||||||
'choice_list' => $choiceList,
|
'choice_list' => $choiceList,
|
||||||
'group_by' => null,
|
'group_by' => null,
|
||||||
);
|
);
|
||||||
|
@ -43,13 +43,6 @@ class ChoiceType extends AbstractType
|
|||||||
throw new FormException('Either the option "choices" or "choice_list" must be set.');
|
throw new FormException('Either the option "choices" or "choice_list" must be set.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$options['choice_list']) {
|
|
||||||
$options['choice_list'] = new SimpleChoiceList(
|
|
||||||
$options['choices'],
|
|
||||||
$options['preferred_choices']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($options['expanded']) {
|
if ($options['expanded']) {
|
||||||
$this->addSubFields($builder, $options['choice_list']->getPreferredViews(), $options);
|
$this->addSubFields($builder, $options['choice_list']->getPreferredViews(), $options);
|
||||||
$this->addSubFields($builder, $options['choice_list']->getRemainingViews(), $options);
|
$this->addSubFields($builder, $options['choice_list']->getRemainingViews(), $options);
|
||||||
@ -62,9 +55,6 @@ class ChoiceType extends AbstractType
|
|||||||
} elseif (false === $options['empty_value']) {
|
} elseif (false === $options['empty_value']) {
|
||||||
// an empty value should be added but the user decided otherwise
|
// an empty value should be added but the user decided otherwise
|
||||||
$emptyValue = null;
|
$emptyValue = null;
|
||||||
} elseif (null === $options['empty_value']) {
|
|
||||||
// user did not made a decision, so we put a blank empty value
|
|
||||||
$emptyValue = $options['required'] ? null : '';
|
|
||||||
} else {
|
} else {
|
||||||
// empty value has been set explicitly
|
// empty value has been set explicitly
|
||||||
$emptyValue = $options['empty_value'];
|
$emptyValue = $options['empty_value'];
|
||||||
@ -155,6 +145,14 @@ class ChoiceType extends AbstractType
|
|||||||
*/
|
*/
|
||||||
public function getDefaultOptions()
|
public function getDefaultOptions()
|
||||||
{
|
{
|
||||||
|
$choiceList = function (Options $options) {
|
||||||
|
return new SimpleChoiceList(
|
||||||
|
// Harden against NULL values (like in EntityType and ModelType)
|
||||||
|
null !== $options['choices'] ? $options['choices'] : array(),
|
||||||
|
$options['preferred_choices']
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
$emptyData = function (Options $options) {
|
$emptyData = function (Options $options) {
|
||||||
if ($options['multiple'] || $options['expanded']) {
|
if ($options['multiple'] || $options['expanded']) {
|
||||||
return array();
|
return array();
|
||||||
@ -163,14 +161,18 @@ class ChoiceType extends AbstractType
|
|||||||
return '';
|
return '';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$emptyValue = function (Options $options) {
|
||||||
|
return $options['required'] ? null : '';
|
||||||
|
};
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'multiple' => false,
|
'multiple' => false,
|
||||||
'expanded' => false,
|
'expanded' => false,
|
||||||
'choice_list' => null,
|
'choice_list' => $choiceList,
|
||||||
'choices' => null,
|
'choices' => array(),
|
||||||
'preferred_choices' => array(),
|
'preferred_choices' => array(),
|
||||||
'empty_data' => $emptyData,
|
'empty_data' => $emptyData,
|
||||||
'empty_value' => null,
|
'empty_value' => $emptyValue,
|
||||||
'error_bubbling' => false,
|
'error_bubbling' => false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ class ChoiceTypeTest extends TypeTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Symfony\Component\Form\Exception\FormException
|
* expectedException Symfony\Component\Form\Exception\FormException
|
||||||
*/
|
*/
|
||||||
public function testEitherChoiceListOrChoicesMustBeSet()
|
public function testEitherChoiceListOrChoicesMustBeSet()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user