Skip to content

Commit

Permalink
ignore _method forms in NativeRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Feb 4, 2019
1 parent 72136f1 commit 4065546
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/NativeRequestHandler.php
Expand Up @@ -115,6 +115,10 @@ public function handleRequest(FormInterface $form, $request = null)
return;
}

if (is_array($data) && array_key_exists('_method', $data) && $method === $data['_method'] && !$form->has('_method')) {
unset($data['_method']);
}

$form->submit($data, 'PATCH' !== $method);
}

Expand Down
53 changes: 53 additions & 0 deletions src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php
Expand Up @@ -179,6 +179,59 @@ public function testMethodOverrideHeaderIgnoredIfNotPost()
$this->assertFalse($form->isSubmitted());
}

public function testFormIgnoresMethodFieldIfRequestMethodIsMatched()
{
$form = $this->createForm('foo', 'PUT', true);
$form->add($this->createForm('bar'));

$this->setRequestData('PUT', [
'foo' => [
'_method' => 'PUT',
'bar' => 'baz',
],
]);

$this->requestHandler->handleRequest($form, $this->request);

$this->assertSame([], $form->getExtraData());
}

public function testFormDoesNotIgnoreMethodFieldIfRequestMethodIsNotMatched()
{
$form = $this->createForm('foo', 'PUT', true);
$form->add($this->createForm('bar'));

$this->setRequestData('PUT', [
'foo' => [
'_method' => 'DELETE',
'bar' => 'baz',
],
]);

$this->requestHandler->handleRequest($form, $this->request);

$this->assertSame(['_method' => 'DELETE'], $form->getExtraData());
}

public function testMethodSubFormIsSubmitted()
{
$form = $this->createForm('foo', 'PUT', true);
$form->add($this->createForm('_method'));
$form->add($this->createForm('bar'));

$this->setRequestData('PUT', [
'foo' => [
'_method' => 'PUT',
'bar' => 'baz',
],
]);

$this->requestHandler->handleRequest($form, $this->request);

$this->assertTrue($form->get('_method')->isSubmitted());
$this->assertSame('PUT', $form->get('_method')->getData());
}

protected function setRequestData($method, $data, $files = [])
{
if ('GET' === $method) {
Expand Down

0 comments on commit 4065546

Please sign in to comment.