bug#9146 [RC][Form] Let null values clear fields in PATCH requests (alex88)
This PR was merged into the master branch.
Discussion
----------
[RC][Form] Let null values clear fields in PATCH requests
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | no
| BC breaks? | yes
| Deprecations? | no
| Tests pass? | yes
I've changed the way form checks wherever or not to submit childs by checking submitted data with array_key_exists and not with just isset.
This way in PATCH requests values are not processed when they are not in array and not also when the value is null. Currently there is no way to null a value with a PATCH request, even passing it null.
This can lead to some BC breaks depending on how users used form in their code.
Commits
-------
f5812c5
[Form] Let null values to unset fields in PATCH requests
This commit is contained in:
commit
554d57b399
@ -548,7 +548,7 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->children as $name => $child) {
|
foreach ($this->children as $name => $child) {
|
||||||
if (isset($submittedData[$name]) || $clearMissing) {
|
if (array_key_exists($name, $submittedData) || $clearMissing) {
|
||||||
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null, $clearMissing);
|
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null, $clearMissing);
|
||||||
unset($submittedData[$name]);
|
unset($submittedData[$name]);
|
||||||
}
|
}
|
||||||
|
@ -82,6 +82,19 @@ class CompoundFormTest extends AbstractFormTest
|
|||||||
$this->assertTrue($form->isValid());
|
$this->assertTrue($form->isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()
|
||||||
|
{
|
||||||
|
$child = $this->getMockForm('firstName');
|
||||||
|
|
||||||
|
$this->form->add($child);
|
||||||
|
|
||||||
|
$child->expects($this->once())
|
||||||
|
->method('submit')
|
||||||
|
->with($this->equalTo(null));
|
||||||
|
|
||||||
|
$this->form->submit(array('firstName' => null), false);
|
||||||
|
}
|
||||||
|
|
||||||
public function testSubmitForwardsNullIfValueIsMissing()
|
public function testSubmitForwardsNullIfValueIsMissing()
|
||||||
{
|
{
|
||||||
$child = $this->getMockForm('firstName');
|
$child = $this->getMockForm('firstName');
|
||||||
|
Reference in New Issue
Block a user