Skip to content

Commit

Permalink
bug #17822 [WIP] [2.7] [Form] fix empty_data option in expanded `Ch…
Browse files Browse the repository at this point in the history
…oiceType` (HeahDude)

This PR was merged into the 2.7 branch.

Discussion
----------

[WIP] [2.7] [Form] fix `empty_data` option in expanded `ChoiceType`

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17791
| License       | MIT
| Doc PR        | -

It might happen because in `Form::submit()` the handling of `empty_data` [line 597](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php#L597) comes after each child of a compound field has been submitted [line 549](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php#L549).

So when `ChoiceType` is `expanded`, `compound` option is defaulted to `true` and it passes its empty submitted data to its children before handling its own `empty_data` option.

This PR uses the listener already added in `ChoiceType` only when `expanded` is true to handle `empty_data` at `PRE_SUBMIT` [line 539](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php#L539).

- [ ] Fix FQCN in tests for 2.8
- [ ] Remove `choices_as_values` in tests for 3.0

Commits
-------

d479adf [Form] fix `empty_data` option in expanded `ChoiceType`
  • Loading branch information
fabpot committed Jun 28, 2016
2 parents d8dd7f6 + d479adf commit 7dbd849
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
use Symfony\Component\Form\Util\FormUtil;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -90,6 +91,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$form = $event->getForm();
$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
// casting it to an array
if (!is_array($data)) {
Expand Down
Expand Up @@ -672,6 +672,66 @@ public function testSubmitSingleNonExpandedObjectChoices()
$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
*/
Expand Down

0 comments on commit 7dbd849

Please sign in to comment.