diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 48cc46fa8acd4..e51746ef93bc4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.1.0 +----- + + * Added `ControllerTrait::isFormValid` + 4.0.0 ----- @@ -39,17 +44,17 @@ CHANGELOG * Deprecated the `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()` methods. * Deprecated `AddCacheClearerPass`, use tagged iterator arguments instead. * Deprecated `AddCacheWarmerPass`, use tagged iterator arguments instead. - * Deprecated `TranslationDumperPass`, use + * Deprecated `TranslationDumperPass`, use `Symfony\Component\Translation\DependencyInjection\TranslationDumperPass` instead - * Deprecated `TranslationExtractorPass`, use + * Deprecated `TranslationExtractorPass`, use `Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass` instead - * Deprecated `TranslatorPass`, use + * Deprecated `TranslatorPass`, use `Symfony\Component\Translation\DependencyInjection\TranslatorPass` instead * Added `command` attribute to the `console.command` tag which takes the command name as value, using it makes the command lazy * Added `cache:pool:prune` command to allow manual stale cache item pruning of supported PSR-6 and PSR-16 cache pool implementations - * Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use + * Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use `Symfony\Component\Translation\Reader\TranslationReader` instead * Deprecated `translation.loader` service, use `translation.reader` instead * `AssetsInstallCommand::__construct()` now takes an instance of diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php index b3d668e6f139d..8352f51a81de2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php @@ -14,6 +14,7 @@ use Psr\Container\ContainerInterface; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\ResponseHeaderBag; @@ -25,6 +26,7 @@ use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Form; use Symfony\Component\Form\FormBuilder; +use Symfony\Component\Form\FormInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Doctrine\Bundle\DoctrineBundle\Registry; @@ -404,6 +406,29 @@ protected function createFormBuilder($data = null, array $options = array()) return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options); } + /** + * Handles request and check form validity + * + * @param FormInterface $form The form to validate + * @param Request|null $request The request + * + * @return bool + * + * @final since version 4.1 + */ + protected function isFormValid(FormInterface $form, Request $request = null) + { + 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 was empty.'); + } + + return $form->handleRequest($request)->isSubmitted() && $form->isValid(); + } + /** * Shortcut to return the Doctrine Registry service. * diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php index e5688af00fbe3..855ceace36d6e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php @@ -13,6 +13,7 @@ use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\JsonResponse; @@ -517,6 +518,77 @@ public function testCreateFormBuilder() $this->assertEquals($formBuilder, $controller->createFormBuilder('foo')); } + 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->once()) + ->method('handleRequest') + ->with($request) + ->willReturn($form) + ; + + $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->once()) + ->method('handleRequest') + ->with($request) + ->willReturn($form) + ; + $form + ->expects($this->once()) + ->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 was 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(); @@ -623,6 +695,11 @@ public function createFormBuilder($data = null, array $options = array()) return parent::createFormBuilder($data, $options); } + public function isFormValid(FormInterface $form, Request $request = null) + { + return parent::isFormValid($form, $request); + } + public function getDoctrine() { return parent::getDoctrine();