Skip to content
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

[Form] ignore _method forms in NativeRequestHandler #30076

Merged
merged 1 commit into from Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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