8000 [From] Fix submitting checkboxes and radios with PATCH method · symfony/symfony@669e394 · GitHub
[go: up one dir, main page]

Skip to content

Commit 669e394

Browse files
committed
[From] Fix submitting checkboxes and radios with PATCH method
1 parent 7ef0951 commit 669e394

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed

src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public function buildView(FormView $view, FormInterface $form, array $options)
5151
*/
5252
public function configureOptions(OptionsResolver $resolver)
5353
{
54-
$emptyData = function (FormInterface $form, $viewData) {
54+
$forceSubmit = function (FormInterface $form, $viewData) {
5555
return $viewData;
5656
};
5757

5858
$resolver->setDefaults(array(
5959
'value' => '1',
60-
'empty_data' => $emptyData,
6160
'compound' => false,
61+
'force_submit' => $forceSubmit, // internal
6262
));
6363
}
6464

src/Symfony/Component/Form/Extension/Core/Type/FormType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public function configureOptions(OptionsResolver $resolver)
215215
'attr' => $defaultAttr,
216216
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
217217
'upload_max_size_message' => $uploadMaxSizeMessage, // internal
218+
'force_submit' => false, // internal
218219
));
219220

220221
$resolver->setAllowedTypes('label_attr', 'array');

src/Symfony/Component/Form/Form.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,11 @@ public function submit($submittedData, $clearMissing = true)
562562

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

566-
if ($isSubmitted || $clearMissing) {
569+
if ($isSubmitted || $clearMissing || $forceSubmit) {
567570
$child->submit($isSubmitted ? $submittedData[$name] : null, $clearMissing);
568571
unset($submittedData[$name]);
569572

src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php

Lines changed: 43 additions & 0 deletions
< 6D47 td data-grid-cell-id="diff-494812c1ce2f832eb544327d9dc3e18ab78d6b1e5b4a0a55ab32369eb3f78eff-138-161-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,49 @@ public function testSubmitWithEmptyValueAndTrueChecked()
136136
$this->assertSame('', $form->getViewData());
137137
}
138138

139+
public function testSubmitNestedCheckBoxWithEmptyValueAndFalseUnchecked()
140+
{
141+
$form = $this->factory->create('form', array(
142+
'check' => true,
143+
))
144+
->add('check', 'checkbox', array(
145+
'value' => '',
146+
))
147+
;
148+
149+
$form->submit(null);
150+
151+
$this->assertFalse($form->get('check')->getData());
152+
$this->assertNull($form->get('check')->getViewData());
153+
}
154+
155+
public function testSubmitNestedCheckBoxWithEmptyValueAndFalseUncheckedWithClearMissingFalse()
156+
{
157+
$form = $this->factory->create('form', array(
158+
'check' => true,
159+
))
160+
->add('check', 'checkbox', array(
161+
'value' => '',
162+
))
163+
;
164+
165+
$form->submit(null, false);
166+
167+
$this->assertFalse($form->get('check')->getData());
168+
$this->assertNull($form->get('check')->getViewData());
169+
}
170+
171+
public function testSubmitNestedCheckboxWithEmptyValueAndTrueCheckedWithClearMissingFalse()
172+
{
173+
$form = $this->factory->create('checkbox', null, array(
174+
'value' => '',
175+
));
176+
$form->submit(true, false);
177+
178+
$this->assertTrue($form->getData());
179+
$this->assertSame('', $form->getViewData());
180+
}
181+
139182
/**
140183
* @dataProvider provideCustomModelTransformerData
141184
*/

src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,4 +2283,43 @@ public function testCustomChoiceTypeDoesNotInheritChoiceLabels()
22832283
// In this case the 'choice_label' closure returns null and not the closure from the first choice type.
22842284
$this->assertNull($form->get('subChoice')->getConfig()->getOption('choice_label'));
22852285
}
2286+
2287+
public function testSubmitDoesMapChoiceDataIfNotClearMissingWhenExpanded()
2288+
{
2289+
$builder = $this->factory->createBuilder('form', array('foo' => 'bar'));
2290+
2291+
$form = $builder->add('foo', 'choice', array(
2292+
'choices' => array(
2293+
'bar' => 'BAR',
2294+
'baz' => 'BAZ',
2295+
),
2296+
'expanded' => true,
2297+
))
2298+
->getForm();
2299+
2300+
$this->assertSame(array('foo' => 'bar'), $form->getData());
2301+
2302+
$form->submit(array('foo' => 'baz'), false);
2303+
2304+
$this->assertSame(array('foo' => 'baz'), $form->getData());
2305+
}
2306+
2307+
public function testSubmitDoesMapChoiceDataIfNotClearMissingWhenCollapsed()
2308+
{
2309+
$builder = $this->factory->createBuilder('form', array('foo' => 'bar'));
2310+
2311+
$form = $builder->add('foo', 'choice', array(
2312+
'choices' => array(
2313+
'bar' => 'BAR',
2314+
'baz' => 'BAZ',
2315+
),
2316+
))
2317+
->getForm();
2318+
2319+
$this->assertSame(array('foo' => 'bar'), $form->getData());
2320+
2321+
$form->submit(array('foo' => 'baz'), false);
2322+
2323+
$this->assertSame(array('foo' => 'baz'), $form->getData());
2324+
}
22862325
}

0 commit comments

Comments
 (0)
0