Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 68b4825

Browse filesBrowse files
bug #30076 [Form] ignore _method forms in NativeRequestHandler (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] ignore _method forms in NativeRequestHandler | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19017 | License | MIT | Doc PR | Commits ------- bc4b091 ignore _method forms in NativeRequestHandler
2 parents a36dc51 + bc4b091 commit 68b4825
Copy full SHA for 68b4825

File tree

2 files changed

+57
-0
lines changed
Filter options

2 files changed

+57
-0
lines changed

‎src/Symfony/Component/Form/NativeRequestHandler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/NativeRequestHandler.php
+4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php
+53Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.