8000 ignore _method forms in NativeRequestHandler · symfony/symfony@32093b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32093b9

Browse files
committed
ignore _method forms in NativeRequestHandler
1 parent 205b0ba commit 32093b9

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/Symfony/Component/Form/NativeRequestHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public function handleRequest(FormInterface $form, $request = null)
115115
return;
116116
}
117117

118+
if (is_array($data) && array_key_exists('_method', $data) && $method === $data['_method'] && !$form->has('_method')) {
119+
unset($data['_method']);
120+
}
121+
118122
$form->submit($data, 'PATCH' !== $method);
119123
}
120124

src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,59 @@ public function testMethodOverrideHeaderIgnoredIfNotPost()
179179
$this->assertFalse($form->isSubmitted());
180180
}
181181

182+
public function testFormIgnoresMethodFieldIfRequestMethodIsMatched()
183+
{
184+
$form = $this->createForm('foo', 'PUT', true);
185+
$form->add($this->createForm('bar'));
186+
187+
$this->setRequestData('PUT', [
188+
'foo' => [
189+
'_method' => 'PUT',
190+
'bar' => 'baz',
191+
],
192+
]);
193+
194+
$this->requestHandler->handleRequest($form, $this->request);
195+
196+
$this->assertSame([], $form->getExtraData());
197+
}
198+
199+
public function testFormDoesNotIgnoreMethodFieldIfRequestMethodIsNotMatched()
200+
{
201+
$form = $this->createForm('foo', 'PUT', true);
202+
$form->add($this->createForm('bar'));
203+
204+
$this->setRequestData('PUT', [
205+
'foo' => [
206+
'_method' => 'DELETE',
207+
'bar' => 'baz',
208+
],
209+
]);
210+
211+
$this->requestHandler->handleRequest($form, $this->request);
212+
213+
$this->assertSame(['_method' => 'DELETE'], $form->getExtraData());
214+
}
215+
216+
public function testMethodSubFormIsSubmitted()
217+
{
218+
$form = $this->createForm('foo', 'PUT', true);
219+
$form->add($this->createForm('_method'));
220+
$form->add($this->createForm('bar'));
221+
222+
$this->setRequestData('PUT', [
223+
'foo' => [
224+
'_method' => 'PUT',
225+
'bar' => 'baz',
226+
],
227+
]);
228+
229+
$this->requestHandler->handleRequest($form, $this->request);
230+
231+
$this->assertTrue($form->get('_method')->isSubmitted());
232+
$this->assertSame('PUT', $form->get('_method')->getData());
233+
}
234+
182235
protected function setRequestData($method, $data, $files = [])
183236
{
184237
if ('GET' === $method) {

0 commit comments

Comments
 (0)
0