Skip to content

Commit

Permalink
[FrameworkBundle] Added ControllerTrait::isFormValid
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Oct 16, 2017
1 parent e4bfc40 commit 3bf20e2
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.1.0
-----

* Added `ControllerTrait::isFormValid`

4.0.0
-----

Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3bf20e2

Please sign in to comment.