Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b4e9ffc

Browse filesBrowse files
committed
[FrameworkBundle] Added ControllerTrait::isFormValid
1 parent 9db435e commit b4e9ffc
Copy full SHA for b4e9ffc

File tree

3 files changed

+97
-5
lines changed
Filter options

3 files changed

+97
-5
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ CHANGELOG
1111
* Deprecated the `Symfony\Bundle\FrameworkBundle\Controller\Controller` class in favor of `Symfony\Bundle\FrameworkBundle\Controller\AbstractController`.
1212
* Enabled autoconfiguration for `Psr\Log\LoggerAwareInterface`
1313
* Added new "auto" mode for `framework.session.cookie_secure` to turn it on when HTTPS is used
14-
* Removed the `framework.messenger.encoder` and `framework.messenger.decoder` options. Use the `framework.messenger.serializer.id` option to replace the Messenger serializer.
14+
* Removed the `framework.messenger.encoder` and `framework.messenger.decoder` options. Use the `framework.messenger.serializer.id` option to replace the Messenger serializer.
1515
* Deprecated the `ContainerAwareCommand` class in favor of `Symfony\Component\Console\Command\Command`
1616
* Made `debug:container` and `debug:autowiring` ignore backslashes in service ids
17+
* Added `ControllerTrait::isFormValid`
1718

1819
4.1.0
1920
-----
@@ -75,17 +76,17 @@ CHANGELOG
7576
* Deprecated the `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()` methods.
7677
* Deprecated `AddCacheClearerPass`, use tagged iterator arguments instead.
7778
* Deprecated `AddCacheWarmerPass`, use tagged iterator arguments instead.
78-
* Deprecated `TranslationDumperPass`, use
79+
* Deprecated `TranslationDumperPass`, use
7980
`Symfony\Component\Translation\DependencyInjection\TranslationDumperPass` instead
80-
* Deprecated `TranslationExtractorPass`, use
81+
* Deprecated `TranslationExtractorPass`, use
8182
`Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass` instead
82-
* Deprecated `TranslatorPass`, use
83+
* Deprecated `TranslatorPass`, use
8384
`Symfony\Component\Translation\DependencyInjection\TranslatorPass` instead
8485
* Added `command` attribute to the `console.command` tag which takes the command
8586
name as value, using it makes the command lazy
8687
* Added `cache:pool:prune` command to allow manual stale cache item pruning of supported PSR-6 and PSR-16 cache pool
8788
implementations
88-
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
89+
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
8990
`Symfony\Component\Translation\Reader\TranslationReader` instead
9091
* Deprecated `translation.loader` service, use `translation.reader` instead
9192
* `AssetsInstallCommand::__construct()` now takes an instance of

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpFoundation\BinaryFileResponse;
2020
use Symfony\Component\HttpFoundation\JsonResponse;
2121
use Symfony\Component\HttpFoundation\RedirectResponse;
22+
use Symfony\Component\HttpFoundation\Request;
2223
use Symfony\Component\HttpFoundation\Response;
2324
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
2425
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -321,6 +322,24 @@ protected function createFormBuilder($data = null, array $options = array()): Fo
321322
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
322323
}
323324

325+
/**
326+
* Handles request and check form validity.
327+
*
328+
* @final
329+
*/
330+
protected function isFormValid(FormInterface $form, Request $request = null): bool
331+
{
332+
if (!$request) {
333+
$request = $this->container->get('request_stack')->getCurrentRequest();
334+
}
335+
336+
if (!$request) {
337+
throw new \LogicException('You must pass a request as second argument because the request stack was empty.');
338+
}
339+
340+
return $form->handleRequest($request)->isSubmitted() && $form->isValid();
341+
}
342+
324343
/**
325344
* Shortcut to return the Doctrine Registry service.
326345
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,77 @@ public function testCreateFormBuilder()
516516
$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
517517
}
518518

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.