[Form] fix empty_data
option in expanded ChoiceType
This commit is contained in:
parent
fad545aff9
commit
d479adf073
@ -33,6 +33,7 @@ use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface as Lega
|
|||||||
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
|
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
|
||||||
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer;
|
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer;
|
||||||
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
|
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
|
||||||
|
use Symfony\Component\Form\Util\FormUtil;
|
||||||
use Symfony\Component\OptionsResolver\Options;
|
use Symfony\Component\OptionsResolver\Options;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
@ -90,6 +91,14 @@ class ChoiceType extends AbstractType
|
|||||||
$form = $event->getForm();
|
$form = $event->getForm();
|
||||||
$data = $event->getData();
|
$data = $event->getData();
|
||||||
|
|
||||||
|
if (null === $data) {
|
||||||
|
$emptyData = $form->getConfig()->getEmptyData();
|
||||||
|
|
||||||
|
if (false === FormUtil::isEmpty($emptyData) && array() !== $emptyData) {
|
||||||
|
$data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Convert the submitted data to a string, if scalar, before
|
// Convert the submitted data to a string, if scalar, before
|
||||||
// casting it to an array
|
// casting it to an array
|
||||||
if (!is_array($data)) {
|
if (!is_array($data)) {
|
||||||
|
@ -672,6 +672,66 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
|||||||
$this->assertTrue($form->isSynchronized());
|
$this->assertTrue($form->isSynchronized());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitSingleChoiceWithEmptyData()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create('choice', null, array(
|
||||||
|
'multiple' => false,
|
||||||
|
'expanded' => false,
|
||||||
|
'choices' => array('test'),
|
||||||
|
'choices_as_values' => true,
|
||||||
|
'empty_data' => 'test',
|
||||||
|
));
|
||||||
|
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame('test', $form->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubmitMultipleChoiceWithEmptyData()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create('choice', null, array(
|
||||||
|
'multiple' => true,
|
||||||
|
'expanded' => false,
|
||||||
|
'choices' => array('test'),
|
||||||
|
'choices_as_values' => true,
|
||||||
|
'empty_data' => array('test'),
|
||||||
|
));
|
||||||
|
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame(array('test'), $form->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubmitSingleChoiceExpandedWithEmptyData()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create('choice', null, array(
|
||||||
|
'multiple' => false,
|
||||||
|
'expanded' => true,
|
||||||
|
'choices' => array('test'),
|
||||||
|
'choices_as_values' => true,
|
||||||
|
'empty_data' => 'test',
|
||||||
|
));
|
||||||
|
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame('test', $form->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubmitMultipleChoiceExpandedWithEmptyData()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create('choice', null, array(
|
||||||
|
'multiple' => true,
|
||||||
|
'expanded' => true,
|
||||||
|
'choices' => array('test'),
|
||||||
|
'choices_as_values' => true,
|
||||||
|
'empty_data' => array('test'),
|
||||||
|
));
|
||||||
|
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame(array('test'), $form->getData());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group legacy
|
* @group legacy
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user