feature #24576 [FrameworkBundle] Added ControllerTrait::isFormValid (lyrixx)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[FrameworkBundle] Added `ControllerTrait::isFormValid`

| Q             | A
| ------------- | ---
| Branch?       | master (4.1)
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/8518

Commits
-------

1a8c844f0e [FrameworkBundle] Added `ControllerTrait::isFormValid`
This commit is contained in:
Fabien Potencier 2019-01-02 13:07:52 +01:00
commit 10b3d0055a
3 changed files with 144 additions and 5 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
4.3.0
-----
* Added `ControllerTrait::isFormValid()`
4.2.0
-----

View File

@ -326,6 +326,28 @@ trait ControllerTrait
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
}
/**
* Handles request and check form validity.
*
* @final
*/
protected function isFormValid(FormInterface $form, Request $request = null): bool
{
if ($form->isSubmitted()) {
throw new \LogicException('The form is already submitted, use $form->isValid() directly.');
}
if (!$request) {
$request = $this->container->get('request_stack')->getCurrentRequest();
}
if (!$request) {
throw new \LogicException('You must pass a request as second argument because the request stack is empty.');
}
return $form->handleRequest($request)->isSubmitted() && $form->isValid();
}
/**
* Shortcut to return the Doctrine Registry service.
*

View File

@ -517,6 +517,117 @@ abstract class ControllerTraitTest extends TestCase
$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage The form is already submitted, use $form->isValid() directly.
*/
public function testIsFormValidWhenAlreadySubmitted()
{
$requestStack = new RequestStack();
$requestStack->push($request = new Request());
$container = new Container();
$container->set('request_stack', $requestStack);
$controller = $this->createController();
$controller->setContainer($container);
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
$form
->expects($this->once())
->method('isSubmitted')
->willReturn(true)
;
$controller->isFormValid($form);
}
public function testIsFormValidWhenInvalid()
{
$requestStack = new RequestStack();
$requestStack->push($request = new Request());
$container = new Container();
$container->set('request_stack', $requestStack);
$controller = $this->createController();
$controller->setContainer($container);
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
$form
->expects($this->at(0))
->method('isSubmitted')
->willReturn(false)
;
$form
->expects($this->once())
->method('handleRequest')
->with($request)
->willReturn($form)
;
$form
->expects($this->at(2))
->method('isSubmitted')
->willReturn(false)
;
$this->assertFalse($controller->isFormValid($form));
}
public function testIsFormValidWhenValid()
{
$requestStack = new RequestStack();
$requestStack->push($request = new Request());
$container = new Container();
$container->set('request_stack', $requestStack);
$controller = $this->createController();
$controller->setContainer($container);
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
$form
->expects($this->at(0))
->method('isSubmitted')
->willReturn(false)
;
$form
->expects($this->once())
->method('handleRequest')
->with($request)
->willReturn($form)
;
$form
->expects($this->at(2))
->method('isSubmitted')
->willReturn(true)
;
$form
->expects($this->once())
->method('isValid')
->willReturn(true)
;
$this->assertTrue($controller->isFormValid($form));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage You must pass a request as second argument because the request stack is empty.
*/
public function testIsFormValidWhenRequestStackIsEmpty()
{
$container = new Container();
$container->set('request_stack', new RequestStack());
$controller = $this->createController();
$controller->setContainer($container);
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
$this->assertTrue($controller->isFormValid($form));
}
public function testGetDoctrine()
{
$doctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
@ -569,5 +680,6 @@ trait TestControllerTrait
createFormBuilder as public;
getDoctrine as public;
addLink as public;
isFormValid as public;
}
}