10000 [Form] ignore _method forms in NativeRequestHandler by xabbuh · Pull Request #30076 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] ignore _method forms in NativeRequestHandler #30076

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

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.
Loading
Jump to
Jump to file
Failed to load files.
8000 Loading
Diff view
Diff view
ignore _method forms in NativeRequestHandler
  • Loading branch information
xabbuh committed Feb 4, 2019
commit bc4b0913b466f76e916fa690c5a43063de7ac418
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/NativeRequestHandler.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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