Skip to content

Commit eabb7a1

Browse files
committed
[Form] Added support for PATCH requests
1 parent e9b6c7c commit eabb7a1

File tree

9 files changed

+62
-29
lines changed

9 files changed

+62
-29
lines changed

src/Symfony/Component/Form/Button.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,14 @@ public function handleRequest($request = null)
358358
/**
359359
* Submits data to the button.
360360
*
361-
* @param null|string $submittedData The data
361+
* @param null|string $submittedData The data.
362+
* @param Boolean $clearMissing Not used.
362363
*
363364
* @return Button The button instance
364365
*
365366
* @throws Exception\AlreadySubmittedException If the button has already been submitted.
366367
*/
367-
public function submit($submittedData)
368+
public function submit($submittedData, $clearMissing = true)
368369
{
369370
if ($this->submitted) {
370371
throw new AlreadySubmittedException('A form can only be submitted once');

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ CHANGELOG
2424
* added methods submit() and isSubmitted() to Form
2525
* deprecated bind() and isBound() in Form
2626
* deprecated AlreadyBoundException in favor of AlreadySubmittedException
27+
* added support for PATCH requests
2728

2829
2.2.0
2930
-----

src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ public function handleRequest(FormInterface $form, $request = null)
7575
return;
7676
}
7777

78-
$form->submit($data);
78+
$form->submit($data, 'PATCH' !== $method);
7979
}
8080
}

src/Symfony/Component/Form/Form.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public function handleRequest($request = null)
464464
/**
465465
* {@inheritdoc}
466466
*/
467-
public function submit($submittedData)
467+
public function submit($submittedData, $clearMissing = true)
468468
{
469469
if ($this->submitted) {
470470
throw new AlreadySubmittedException('A form can only be submitted once');
@@ -518,8 +518,10 @@ public function submit($submittedData)
518518
}
519519

520520
foreach ($this->children as $name => $child) {
521-
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null);
522-
unset($submittedData[$name]);
521+
if (isset($submittedData[$name]) || $clearMissing) {
522+
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null, $clearMissing);
523+
unset($submittedData[$name]);
524+
}
523525
}
524526

525527
$this->extraData = $submittedData;

src/Symfony/Component/Form/FormInterface.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,16 @@ public function handleRequest($request = null);
242242
/**
243243
* Submits data to the form, transforms and validates it.
244244
*
245-
* @param null|string|array $submittedData The submitted data.
245+
* @param null|string|array $submittedData The submitted data.
246+
* @param Boolean $clearMissing Whether to set fields to NULL
247+
* when they are missing in the
248+
* submitted data.
246249
*
247250
* @return FormInterface The form instance
248251
*
249252
* @throws Exception\AlreadySubmittedException If the form has already been submitted.
250253
*/
251-
public function submit($submittedData);
254+
public function submit($submittedData, $clearMissing = true);
252255

253256
/**
254257
* Returns the root of the form tree.

src/Symfony/Component/Form/NativeRequestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function handleRequest(FormInterface $form, $request = null)
9090
return;
9191
}
9292

93-
$form->submit($data);
93+
$form->submit($data, 'PATCH' !== $method);
9494
}
9595

9696
/**

src/Symfony/Component/Form/SubmitButton.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ public function isClicked()
3434
/**
3535
* Submits data to the button.
3636
*
37-
* @param null|string $submittedData The data
37+
* @param null|string $submittedData The data.
38+
* @param Boolean $clearMissing Not used.
3839
*
3940
* @return SubmitButton The button instance
4041
*
4142
* @throws Exception\AlreadySubmittedException If the form has already been submitted.
4243
*/
43-
public function submit($submittedData)
44+
public function submit($submittedData, $clearMissing = true)
4445
{
45-
parent::submit($submittedData);
46+
parent::submit($submittedData, $clearMissing);
4647

4748
$this->clicked = null !== $submittedData;
4849

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public function testSubmitIfNameInRequest($method)
5858
));
5959

6060
$form->expects($this->once())
61-
->method('Submit')
62-
->with('DATA');
61+
->method('submit')
62+
->with('DATA', 'PATCH' !== $method);
6363

6464
$this->requestHandler->handleRequest($form, $this->request);
6565
}
@@ -78,7 +78,7 @@ public function testDoNotSubmitIfWrongRequestMethod($method)
7878
));
7979

8080
$form->expects($this->never())
81-
->method('Submit');
81+
->method('submit');
8282

8383
$this->requestHandler->handleRequest($form, $this->request);
8484
}
@@ -95,8 +95,8 @@ public function testSubmitSimpleFormWithNullIfNameNotInRequestAndNotGetRequest($
9595
));
9696

9797
$form->expects($this->once())
98-
->method('Submit')
99-
->with($this->identicalTo(null));
98+
->method('submit')
99+
->with($this->identicalTo(null), 'PATCH' !== $method);
100100

101101
$this->requestHandler->handleRequest($form, $this->request);
102102
}
@@ -113,8 +113,8 @@ public function testSubmitCompoundFormWithArrayIfNameNotInRequestAndNotGetReques
113113
));
114114

115115
$form->expects($this->once())
116-
->method('Submit')
117-
->with($this->identicalTo(array()));
116+
->method('submit')
117+
->with($this->identicalTo(array()), 'PATCH' !== $method);
118118

119119
$this->requestHandler->handleRequest($form, $this->request);
120120
}
@@ -128,7 +128,7 @@ public function testDoNotSubmitIfNameNotInRequestAndGetRequest()
128128
));
129129

130130
$form->expects($this->never())
131-
->method('Submit');
131+
->method('submit');
132132

133133
$this->requestHandler->handleRequest($form, $this->request);
134134
}
@@ -152,8 +152,8 @@ public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method)
152152
));
153153

154154
$form->expects($this->once())
155-
->method('Submit')
156-
->with($requestData);
155+
->method('submit')
156+
->with($requestData, 'PATCH' !== $method);
157157

158158
$this->requestHandler->handleRequest($form, $this->request);
159159
}
@@ -176,7 +176,7 @@ public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method)
176176
));
177177

178178
$form->expects($this->never())
179-
->method('Submit');
179+
->method('submit');
180180

181181
$this->requestHandler->handleRequest($form, $this->request);
182182
}
@@ -200,11 +200,11 @@ public function testMergeParamsAndFiles($method)
200200
));
201201

202202
$form->expects($this->once())
203-
->method('Submit')
203+
->method('submit')
204204
->with(array(
205205
'field1' => 'DATA',
206206
'field2' => $file,
207-
));
207+
), 'PATCH' !== $method);
208208

209209
$this->requestHandler->handleRequest($form, $this->request);
210210
}
@@ -224,8 +224,8 @@ public function testParamTakesPrecedenceOverFile($method)
224224
));
225225

226226
$form->expects($this->once())
227-
->method('Submit')
228-
->with('DATA');
227+
->method('submit')
228+
->with('DATA', 'PATCH' !== $method);
229229

230230
$this->requestHandler->handleRequest($form, $this->request);
231231
}
@@ -245,8 +245,8 @@ public function testSubmitFileIfNoParam($method)
245245
));
246246

247247
$form->expects($this->once())
248-
->method('Submit')
249-
->with($file);
248+
->method('submit')
249+
->with($file, 'PATCH' !== $method);
250250

251251
$this->requestHandler->handleRequest($form, $this->request);
252252
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ public function testSubmitForwardsNullIfValueIsMissing()
5858
$this->form->submit(array());
5959
}
6060

61+
public function testSubmitDoesNotForwardNullIfNotClearMissing()
62+
{
63+
$child = $this->getMockForm('firstName');
64+
65+
$this->form->add($child);
66+
67+
$child->expects($this->never())
68+
->method('submit');
69+
70+
$this->form->submit(array(), false);
71+
}
72+
73+
public function testClearMissingFlagIsForwarded()
74+
{
75+
$child = $this->getMockForm('firstName');
76+
77+
$this->form->add($child);
78+
79+
$child->expects($this->once())
80+
->method('submit')
81+
->with($this->equalTo('foo'), false);
82+
83+
$this->form->submit(array('firstName' => 'foo'), false);
84+
}
85+
6186
public function testCloneChildren()
6287
{
6388
$child = $this->getBuilder('child')->getForm();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy