merged branch bschussek/issue1341 (PR #7849)

This PR was merged into the master branch.

Discussion
----------

[Form] Added support for PATCH requests

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #1341
| License       | MIT
| Doc PR        | not needed

Commits
-------

eabb7a1 [Form] Added support for PATCH requests
This commit is contained in:
Fabien Potencier 2013-04-25 16:47:21 +02:00
commit 1099858678
9 changed files with 62 additions and 29 deletions

View File

@ -358,13 +358,14 @@ class Button implements \IteratorAggregate, FormInterface
/** /**
* Submits data to the button. * 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 * @return Button The button instance
* *
* @throws Exception\AlreadySubmittedException If the button has already been submitted. * @throws Exception\AlreadySubmittedException If the button has already been submitted.
*/ */
public function submit($submittedData) public function submit($submittedData, $clearMissing = true)
{ {
if ($this->submitted) { if ($this->submitted) {
throw new AlreadySubmittedException('A form can only be submitted once'); throw new AlreadySubmittedException('A form can only be submitted once');

View File

@ -24,6 +24,7 @@ CHANGELOG
* added methods submit() and isSubmitted() to Form * added methods submit() and isSubmitted() to Form
* deprecated bind() and isBound() in Form * deprecated bind() and isBound() in Form
* deprecated AlreadyBoundException in favor of AlreadySubmittedException * deprecated AlreadyBoundException in favor of AlreadySubmittedException
* added support for PATCH requests
2.2.0 2.2.0
----- -----

View File

@ -75,6 +75,6 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface
return; return;
} }
$form->submit($data); $form->submit($data, 'PATCH' !== $method);
} }
} }

View File

@ -464,7 +464,7 @@ class Form implements \IteratorAggregate, FormInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function submit($submittedData) public function submit($submittedData, $clearMissing = true)
{ {
if ($this->submitted) { if ($this->submitted) {
throw new AlreadySubmittedException('A form can only be submitted once'); throw new AlreadySubmittedException('A form can only be submitted once');
@ -518,8 +518,10 @@ class Form implements \IteratorAggregate, FormInterface
} }
foreach ($this->children as $name => $child) { foreach ($this->children as $name => $child) {
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null); if (isset($submittedData[$name]) || $clearMissing) {
unset($submittedData[$name]); $child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null, $clearMissing);
unset($submittedData[$name]);
}
} }
$this->extraData = $submittedData; $this->extraData = $submittedData;

View File

@ -242,13 +242,16 @@ interface FormInterface extends \ArrayAccess, \Traversable, \Countable
/** /**
* Submits data to the form, transforms and validates it. * 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 * @return FormInterface The form instance
* *
* @throws Exception\AlreadySubmittedException If the form has already been submitted. * @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. * Returns the root of the form tree.

View File

@ -90,7 +90,7 @@ class NativeRequestHandler implements RequestHandlerInterface
return; return;
} }
$form->submit($data); $form->submit($data, 'PATCH' !== $method);
} }
/** /**

View File

@ -34,15 +34,16 @@ class SubmitButton extends Button implements ClickableInterface
/** /**
* Submits data to the button. * 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 * @return SubmitButton The button instance
* *
* @throws Exception\AlreadySubmittedException If the form has already been submitted. * @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; $this->clicked = null !== $submittedData;

View File

@ -58,8 +58,8 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->once()) $form->expects($this->once())
->method('Submit') ->method('submit')
->with('DATA'); ->with('DATA', 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -78,7 +78,7 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->never()) $form->expects($this->never())
->method('Submit'); ->method('submit');
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -95,8 +95,8 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->once()) $form->expects($this->once())
->method('Submit') ->method('submit')
->with($this->identicalTo(null)); ->with($this->identicalTo(null), 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -113,8 +113,8 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->once()) $form->expects($this->once())
->method('Submit') ->method('submit')
->with($this->identicalTo(array())); ->with($this->identicalTo(array()), 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -128,7 +128,7 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->never()) $form->expects($this->never())
->method('Submit'); ->method('submit');
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -152,8 +152,8 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->once()) $form->expects($this->once())
->method('Submit') ->method('submit')
->with($requestData); ->with($requestData, 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -176,7 +176,7 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->never()) $form->expects($this->never())
->method('Submit'); ->method('submit');
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -200,11 +200,11 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->once()) $form->expects($this->once())
->method('Submit') ->method('submit')
->with(array( ->with(array(
'field1' => 'DATA', 'field1' => 'DATA',
'field2' => $file, 'field2' => $file,
)); ), 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -224,8 +224,8 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->once()) $form->expects($this->once())
->method('Submit') ->method('submit')
->with('DATA'); ->with('DATA', 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }
@ -245,8 +245,8 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
)); ));
$form->expects($this->once()) $form->expects($this->once())
->method('Submit') ->method('submit')
->with($file); ->with($file, 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request); $this->requestHandler->handleRequest($form, $this->request);
} }

View File

@ -58,6 +58,31 @@ class CompoundFormTest extends AbstractFormTest
$this->form->submit(array()); $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() public function testCloneChildren()
{ {
$child = $this->getBuilder('child')->getForm(); $child = $this->getBuilder('child')->getForm();