8000 [Form] Fix submitting checkboxes and radios with PATCH method by HeahDude · Pull Request #17771 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix submitting checkboxes and radios with PATCH method #17771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransformer;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CheckboxType extends AbstractType
Expand Down Expand Up @@ -57,9 +58,14 @@ public function configureOptions(OptionsResolver $resolver)

$resolver->setDefaults(array(
'value' => '1',
'empty_data' => $emptyData,
'compound' => false,
'empty_data' => $emptyData,
));

$resolver->setNormalizer('force_submit', function (Options $options) {
// If pre set data is true, we need to ensure that $emptyData will be submitted
return isset($options['data']) && (bool) $options['data'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

return !empty($options['data']);

instead?

});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public function configureOptions(OptionsResolver $resolver)
'attr' => $defaultAttr,
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
'upload_max_size_message' => $uploadMaxSizeMessage, // internal
'force_submit' => false, // internal
));

$resolver->setAllowedTypes('label_attr', 'array');
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,11 @@ public function submit($submittedData, $clearMissing = true)

foreach ($this->children as $name => $child) {
$isSubmitted = array_key_exists($name, $submittedData);
// Some form types like {@link \Symfony\Component\Form\Extension\Core\Type\CheckboxType}
// need to force submission of null, so the form is properly updated.
$forceSubmit = $child->getConfig()->getOption('force_submit', false);

if ($isSubmitted || $clearMissing) {
if ($isSubmitted || $clearMissing || $forceSubmit) {
$child->submit($isSubmitted ? $submittedData[$name] : null, $clearMissing);
unset($submittedData[$name]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,49 @@ public function testSubmitWithEmptyValueAndTrueChecked()
$this->assertSame('', $form->getViewData());
}

public function testSubmitNestedCheckBoxWithEmptyValueAndFalseUnchecked()
{
$form = $this->factory->create('form', array(
'check' => true,
))
->add('check', 'checkbox', array(
'value' => '',
))
;

$form->submit(null);

$this->assertFalse($form->get('check')->getData());
$this->assertNull($form->get('check')->getViewData());
}

public function testSubmitNestedCheckBoxWithEmptyValueAndFalseUncheckedWithClearMissingFalse()
{
$form = $this->factory->create('form', array(
'check' => true,
))
->add('check', 'checkbox', array(
'value' => '',
))
;

$form->submit(null, false);

$this->assertFalse($form->get('check')->getData());
$this->assertNull($form->get('check')->getViewData());
}

public function testSubmitNestedCheckboxWithEmptyValueAndTrueCheckedWithClearMissingFalse()
{
$form = $this->factory->create('checkbox', null, array(
'value' => '',
));
$form->submit(true, false);

$this->assertTrue($form->getData());
$this->assertSame('', $form->getViewData());
}

/**
* @dataProvider provideCustomModelTransformerData
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2283,4 +2283,43 @@ public function testCustomChoiceTypeDoesNotInheritChoiceLabels()
// In this case the 'choice_label' closure returns null and not the closure from the first choice type.
$this->assertNull($form->get('subChoice')->getConfig()->getOption('choice_label'));
}

public function testSubmitDoesMapChoiceDataIfNotClearMissingWhenExpanded()
{
$builder = $this->factory->createBuilder('form', array('foo' => 'bar'));

$form = $builder->add('foo', 'choice', array(
'choices' => array(
'bar' => 'BAR',
'baz' => 'BAZ',
),
'expanded' => true,
))
->getForm();

$this->assertSame(array('foo' => 'bar'), $form->getData());

$form->submit(array('foo' => 'baz'), false);

$this->assertSame(array('foo' => 'baz'), $form->getData());
}

public function testSubmitDoesMapChoiceDataIfNotClearMissingWhenCollapsed()
{
$builder = $this->factory->createBuilder('form', array('foo' => 'bar'));

$form = $builder->add('foo', 'choice', array(
'choices' => array(
'bar' => 'BAR',
'baz' => 'BAZ',
),
))
->getForm();

$this->assertSame(array('foo' => 'bar'), $form->getData());

$form->submit(array('foo' => 'baz'), false);

$this->assertSame(array('foo' => 'baz'), $form->getData());
}
}
0