Skip to content

[Form] Added support for PATCH requests #7849

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
Apr 25, 2013
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.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,14 @@ public function handleRequest($request = null)
/**
* Submits data to the button.
*
* @param null|string $submittedData The data
* @param null|string $submittedData The data.
* @param Boolean $clearMissing Not used.
*
* @return Button The button instance
*
* @throws Exception\AlreadySubmittedException If the button has already been submitted.
*/
public function submit($submittedData)
public function submit($submittedData, $clearMissing = true)
{
if ($this->submitted) {
throw new AlreadySubmittedException('A form can only be submitted once');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CHANGELOG
* added methods submit() and isSubmitted() to Form
* deprecated bind() and isBound() in Form
* deprecated AlreadyBoundException in favor of AlreadySubmittedException
* added support for PATCH requests

2.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ public function handleRequest(FormInterface $form, $request = null)
return;
}

$form->submit($data);
$form->submit($data, 'PATCH' !== $method);
}
}
8 changes: 5 additions & 3 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public function handleRequest($request = null)
/**
* {@inheritdoc}
*/
public function submit($submittedData)
public function submit($submittedData, $clearMissing = true)
{
if ($this->submitted) {
throw new AlreadySubmittedException('A form can only be submitted once');
Expand Down Expand Up @@ -518,8 +518,10 @@ public function submit($submittedData)
}

foreach ($this->children as $name => $child) {
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null);
unset($submittedData[$name]);
if (isset($submittedData[$name]) || $clearMissing) {
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null, $clearMissing);
unset($submittedData[$name]);
}
}

$this->extraData = $submittedData;
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Form/FormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,16 @@ public function handleRequest($request = null);
/**
* Submits data to the form, transforms and validates it.
*
* @param null|string|array $submittedData The submitted data.
* @param null|string|array $submittedData The submitted data.
* @param Boolean $clearMissing Whether to set fields to NULL
* when they are missing in the
* submitted data.
*
* @return FormInterface The form instance
*
* @throws Exception\AlreadySubmittedException If the form has already been submitted.
*/
public function submit($submittedData);
public function submit($submittedData, $clearMissing = true);

/**
* Returns the root of the form tree.
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/NativeRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function handleRequest(FormInterface $form, $request = null)
return;
}

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

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/Form/SubmitButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ public function isClicked()
/**
* Submits data to the button.
*
* @param null|string $submittedData The data
* @param null|string $submittedData The data.
* @param Boolean $clearMissing Not used.
*
* @return SubmitButton The button instance
*
* @throws Exception\AlreadySubmittedException If the form has already been submitted.
*/
public function submit($submittedData)
public function submit($submittedData, $clearMissing = true)
{
parent::submit($submittedData);
parent::submit($submittedData, $clearMissing);

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

Expand Down
34 changes: 17 additions & 17 deletions src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function testSubmitIfNameInRequest($method)
));

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

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -78,7 +78,7 @@ public function testDoNotSubmitIfWrongRequestMethod($method)
));

$form->expects($this->never())
->method('Submit');
->method('submit');

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -95,8 +95,8 @@ public function testSubmitSimpleFormWithNullIfNameNotInRequestAndNotGetRequest($
));

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

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -113,8 +113,8 @@ public function testSubmitCompoundFormWithArrayIfNameNotInRequestAndNotGetReques
));

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

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -128,7 +128,7 @@ public function testDoNotSubmitIfNameNotInRequestAndGetRequest()
));

$form->expects($this->never())
->method('Submit');
->method('submit');

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -152,8 +152,8 @@ public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method)
));

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

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -176,7 +176,7 @@ public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method)
));

$form->expects($this->never())
->method('Submit');
->method('submit');

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -200,11 +200,11 @@ public function testMergeParamsAndFiles($method)
));

$form->expects($this->once())
->method('Submit')
->method('submit')
->with(array(
'field1' => 'DATA',
'field2' => $file,
));
), 'PATCH' !== $method);

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -224,8 +224,8 @@ public function testParamTakesPrecedenceOverFile($method)
));

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

$this->requestHandler->handleRequest($form, $this->request);
}
Expand All @@ -245,8 +245,8 @@ public function testSubmitFileIfNoParam($method)
));

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

$this->requestHandler->handleRequest($form, $this->request);
}
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ public function testSubmitForwardsNullIfValueIsMissing()
$this->form->submit(array());
}

public function testSubmitDoesNotForwardNullIfNotClearMissing()
{
$child = $this->getMockForm('firstName');

$this->form->add($child);

$child->expects($this->never())
->method('submit');

$this->form->submit(array(), false);
}

public function testClearMissingFlagIsForwarded()
{
$child = $this->getMockForm('firstName');

$this->form->add($child);

$child->expects($this->once())
->method('submit')
->with($this->equalTo('foo'), false);

$this->form->submit(array('firstName' => 'foo'), false);
}

public function testCloneChildren()
{
$child = $this->getBuilder('child')->getForm();
Expand Down