ignore _method forms in NativeRequestHandler

This commit is contained in:
Christian Flothmann 2019-02-04 19:26:43 +01:00
parent 205b0ba2cc
commit bc4b0913b4
2 changed files with 57 additions and 0 deletions

View File

@ -115,6 +115,10 @@ class NativeRequestHandler implements RequestHandlerInterface
return;
}
if (\is_array($data) && array_key_exists('_method', $data) && $method === $data['_method'] && !$form->has('_method')) {
unset($data['_method']);
}
$form->submit($data, 'PATCH' !== $method);
}

View File

@ -179,6 +179,59 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest
$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) {