8000 [FrameworkBundle] Added `ControllerTrait::isFormValid` · symfony/symfony@3bf20e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3bf20e2

Browse files
committed
[FrameworkBundle] Added ControllerTrait::isFormValid
1 parent e4bfc40 commit 3bf20e2

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* Added `ControllerTrait::isFormValid`
8+
49
4.0.0
510
-----
611

@@ -39,17 +44,17 @@ CHANGELOG
3944
* Deprecated the `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()` methods.
4045
* Deprecated `AddCacheClearerPass`, use tagged iterator arguments instead.
4146
* Deprecated `AddCacheWarmerPass`, use tagged iterator arguments instead.
42-
* Deprecated `TranslationDumperPass`, use
47+
* Deprecated `TranslationDumperPass`, use
4348
`Symfony\Component\Translation\DependencyInjection\TranslationDumperPass` instead
44-
* Deprecated `TranslationExtractorPass`, use
49+
* Deprecated `TranslationExtractorPass`, use
4550
`Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass` instead
46-
* Deprecated `TranslatorPass`, use
51+
* Deprecated `TranslatorPass`, use
4752
`Symfony\Component\Translation\DependencyInjection\TranslatorPass` instead
4853
* Added `command` attribute to the `console.command` tag which takes the command
4954
name as value, using it makes the command lazy
5055
* Added `cache:pool:prune` command to allow manual stale cache item pruning of supported PSR-6 and PSR-16 cache pool
5156
implementations
52-
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
57+
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
5358
`Symfony\Component\Translation\Reader\TranslationReader` instead
5459
* Deprecated `translation.loader` service, use `translation.reader` instead
5560
* `AssetsInstallCommand::__construct()` now takes an instance of

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1616
use Symfony\Component\HttpFoundation\JsonResponse;
17+
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
1819
use Symfony\Component\HttpFoundation\RedirectResponse;
1920
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
@@ -25,6 +26,7 @@
2526
use Symfony\Component\Form\Extension\Core\Type\FormType;
2627
use Symfony\Component\Form\Form;
2728
use Symfony\Component\Form\FormBuilder;
29+
use Symfony\Component\Form\FormInterface;
2830
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2931
use Doctrine\Bundle\DoctrineBundle\Registry;
3032

@@ -404,6 +406,29 @@ protected function createFormBuilder($data = null, array $options = array())
404406
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
405407
}
406408

409+
/**
410+
* Handles request and check form validity
411+
*
412+
* @param FormInterface $form The form to validate
413+
* @param Request|null $request The request
414+
*
415+
* @return bool
416+
*
417+
* @final since version 4.1
418+
*/
419+
protected function isFormValid(FormInterface $form, Request $request = null)
420+
{
421+
if (!$request) {
422+
$request = $this->container->get('request_stack')->getCurrentRequest();
423+
}
424+
425+
if (!$request) {
426+
throw new \LogicException('You must pass a request as second argument because the request stack was empty.');
427+
}
428+
429+
return $form->handleRequest($request)->isSubmitted() && $form->isValid();
430+
}
431+
407432
/**
408433
* Shortcut to return the Doctrine Registry service.
409434
*

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1515
use Symfony\Component\DependencyInjection\Container;
16+
use Symfony\Component\Form\FormInterface;
1617
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1718
use Symfony\Component\HttpFoundation\File\File;
1819
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -517,6 +518,77 @@ public function testCreateFormBuilder()
517518
$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
518519
}
519520

521+
public function testIsFormValidWhenInvalid()
522+
{
523+
$requestStack = new RequestStack();
524+
$requestStack->push($request = new Request());
525+
526+
$container = new Container();
527+
$container->set('request_stack', $requestStack);
528+
529+
$controller = $this->createController();
530+
$controller->setContainer($container);
531+
532+
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
533+
$form
534+
->expects($this->once())
535+
->method('handleRequest')
536+
->with($request)
537+
->willReturn($form)
538+
;
539+
540+
$this->assertFalse($controller->isFormValid($form));
541+
}
542+
543+
public function testIsFormValidWhenValid()
544+
{
545+
$requestStack = new RequestStack();
546+
$requestStack->push($request = new Request());
547+
548+
$container = new Container();
549+
$container->set('request_stack', $requestStack);
550+
551+
$controller = $this->createController();
552+
$controller->setContainer($container);
553+
554+
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
555+
$form
556+
->expects($this->once())
557+
->method('handleRequest')
558+
->with($request)
559+
->willReturn($form)
560+
;
561+
$form
562+
->expects($this->once())
563+
->method('isSubmitted')
564+
->willReturn(true)
565+
;
566+
$form
567+
->expects($this->once())
568+
->method('isValid')
569+
->willReturn(true)
570+
;
571+
572+
$this->assertTrue($controller->isFormValid($form));
573+
}
574+
575+
/**
576+
* @expectedException \LogicException
577+
* @expectedExceptionMessage You must pass a request as second argument because the request stack was empty.
578+
*/
579+
public function testIsFormValidWhenRequestStackIsEmpty()
580+
{
581+
$container = new Container();
582+
$container->set('request_stack', new RequestStack());
583+
584+
$controller = $this->createController();
585+
$controller->setContainer($container);
586+
587+
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
588+
589+
$this->assertTrue($controller->isFormValid($form));
590+
}
591+
520592
public function testGetDoctrine()
521593
{
522594
$doctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
@@ -623,6 +695,11 @@ public function createFormBuilder($data = null, array $options = array())
623695
return parent::createFormBuilder($data, $options);
624696
}
625697

698+
public function isFormValid(FormInterface $form, Request $request = null)
699+
{
700+
return parent::isFormValid($form, $request);
701+
}
702+
626703
public function getDoctrine()
627704
{
628705
return parent::getDoctrine();

0 commit comments

Comments
 (0)
0