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 e029e62

Browse filesBrowse files
committed
[FrameworkBundle] Added ControllerTrait::isFormValid
1 parent 100f205 commit e029e62
Copy full SHA for e029e62

File tree

3 files changed

+144
-5
lines changed
Filter options

3 files changed

+144
-5
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+10-5Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* Added `ControllerTrait::isFormValid()`
8+
49
4.2.0
510
-----
611

@@ -12,7 +17,7 @@ CHANGELOG
1217
* Deprecated the `Symfony\Bundle\FrameworkBundle\Controller\Controller` class in favor of `Symfony\Bundle\FrameworkBundle\Controller\AbstractController`.
1318
* Enabled autoconfiguration for `Psr\Log\LoggerAwareInterface`
1419
* Added new "auto" mode for `framework.session.cookie_secure` to turn it on when HTTPS is used
15-
* Removed the `framework.messenger.encoder` and `framework.messenger.decoder` options. Use the `framework.messenger.serializer.id` option to replace the Messenger serializer.
20+
* Removed the `framework.messenger.encoder` and `framework.messenger.decoder` options. Use the `framework.messenger.serializer.id` option to replace the Messenger serializer.
1621
* Deprecated the `ContainerAwareCommand` class in favor of `Symfony\Component\Console\Command\Command`
1722
* Made `debug:container` and `debug:autowiring` ignore backslashes in service ids
1823
* Deprecated the `Templating\Helper\TranslatorHelper::transChoice()` method, use the `trans()` one instead with a `%count%` parameter
@@ -83,17 +88,17 @@ CHANGELOG
8388
* Deprecated the `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()` methods.
8489
* Deprecated `AddCacheClearerPass`, use tagged iterator arguments instead.
8590
* Deprecated `AddCacheWarmerPass`, use tagged iterator arguments instead.
86-
* Deprecated `TranslationDumperPass`, use
91+
* Deprecated `TranslationDumperPass`, use
8792
`Symfony\Component\Translation\DependencyInjection\TranslationDumperPass` instead
88-
* Deprecated `TranslationExtractorPass`, use
93+
* Deprecated `TranslationExtractorPass`, use
8994
`Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass` instead
90-
* Deprecated `TranslatorPass`, use
95+
* Deprecated `TranslatorPass`, use
9196
`Symfony\Component\Translation\DependencyInjection\TranslatorPass` instead
9297
* Added `command` attribute to the `console.command` tag which takes the command
9398
name as value, using it makes the command lazy
9499
* Added `cache:pool:prune` command to allow manual stale cache item pruning of supported PSR-6 and PSR-16 cache pool
95100
implementations
96-
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
101+
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
97102
`Symfony\Component\Translation\Reader\TranslationReader` instead
98103
* Deprecated `translation.loader` service, use `translation.reader` instead
99104
* `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
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,28 @@ protected function createFormBuilder($data = null, array $options = array()): Fo
326326
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
327327
}
328328

329+
/**
330+
* Handles request and check form validity.
331+
*
332+
* @final
333+
*/
334+
protected function isFormValid(FormInterface $form, Request $request = null): bool
335+
{
336+
if ($form->isSubmitted()) {
337+
throw new \LogicException('The form is already submitted, use $form->isValid() directly.');
338+
}
339+
340+
if (!$request) {
341+
$request = $this->container->get('request_stack')->getCurrentRequest();
342+
}
343+
344+
if (!$request) {
345+
throw new \LogicException('You must pass a request as second argument because the request stack was empty.');
346+
}
347+
348+
return $form->handleRequest($request)->isSubmitted() && $form->isValid();
349+
}
350+
329351
/**
330352
* Shortcut to return the Doctrine Registry service.
331353
*

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

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

520+
/**
521+
* @expectedException \LogicException
522+
* @expectedExceptionMessage The form is already submitted, use $form->isValid() directly.
523+
*/
524+
public function testIsFormValidWhenAlreadySubmitted()
525+
{
526+
$requestStack = new RequestStack();
527+
$requestStack->push($request = new Request());
528+
529+
$container = new Container();
530+
$container->set('request_stack', $requestStack);
531+
532+
$controller = $this->createController();
533+
$controller->setContainer($container);
534+
535+
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
536+
$form
537+
->expects($this->once())
538+
->method('isSubmitted')
539+
->willReturn(true)
540+
;
541+
542+
$controller->isFormValid($form);
543+
}
544+
545+
public function testIsFormValidWhenInvalid()
546+
{
547+
$requestStack = new RequestStack();
548+
$requestStack->push($request = new Request());
549+
550+
$container = new Container();
551+
$container->set('request_stack', $requestStack);
552+
553+
$controller = $this->createController();
554+
$controller->setContainer($container);
555+
556+
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
557+
$form
558+
->expects($this->at(0))
559+
->method('isSubmitted')
560+
->willReturn(false)
561+
;
562+
$form
563+
->expects($this->once())
564+
->method('handleRequest')
565+
->with($request)
566+
->willReturn($form)
567+
;
568+
$form
569+
->expects($this->at(2))
570+
->method('isSubmitted')
571+
->willReturn(false)
572+
;
573+
574+
$this->assertFalse($controller->isFormValid($form));
575+
}
576+
577+
public function testIsFormValidWhenValid()
578+
{
579+
$requestStack = new RequestStack();
580+
$requestStack->push($request = new Request());
581+
582+
$container = new Container();
583+
$container->set('request_stack', $requestStack);
584+
585+
$controller = $this->createController();
586+
$controller->setContainer($container);
587+
588+
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
589+
$form
590+
->expects($this->at(0))
591+
->method('isSubmitted')
592+
->willReturn(false)
593+
;
594+
$form
595+
->expects($this->once())
596+
->method('handleRequest')
597+
->with($request)
598+
->willReturn($form)
599+
;
600+
$form
601+
->expects($this->at(2))
602+
->method('isSubmitted')
603+
->willReturn(true)
604+
;
605+
$form
606+
->expects($this->once())
607+
->method('isValid')
608+
->willReturn(true)
609+
;
610+
611+
$this->assertTrue($controller->isFormValid($form));
612+
}
613+
614+
/**
615+
* @expectedException \LogicException
616+
* @expectedExceptionMessage You must pass a request as second argument because the request stack was empty.
617+
*/
618+
public function testIsFormValidWhenRequestStackIsEmpty()
619+
{
620+
$container = new Container();
621+
$container->set('request_stack', new RequestStack());
622+
623+
$controller = $this->createController();
624+
$controller->setContainer($container);
625+
626+
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
627+
628+
$this->assertTrue($controller->isFormValid($form));
629+
}
630+
520631
public function testGetDoctrine()
521632
{
522633
$doctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
@@ -569,5 +680,6 @@ trait TestControllerTrait
569680
createFormBuilder as public;
570681
getDoctrine as public;
571682
addLink as public;
683+
isFormValid as public;
572684
}
573685
}

0 commit comments

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