Skip to content

[FrameworkBundle] add isSubmittedFormValid method to AbstractController #54743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CHANGELOG
* Attach the workflow's configuration to the `workflow` tag
* Add the `allowed_recipients` option for mailer to allow some users to receive
emails even if `recipients` is defined.
* Add `isSubmittedFormValid` method into `AbstractController` make a shortcut of the `FormInterface`'s three functions `handleRequest()`, `isSubmitted()` and `isValid()`

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ protected function sendEarlyHints(iterable $links = [], ?Response $response = nu
return $response;
}

/**
* Call FormInterface::handleRequest() and return the result of the condition FormInterface::isSubmitted() and FormInterface::isValid().
*/
protected function isSubmittedFormValid(FormInterface $form): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't like this alternative. It makes it less obvious that we're dealing with a request here.

Since all methods involved concern the form only, I don't see why the controller should communicate with the form when the form can communicate directly with the request, making this shortcut helpful for controllers not extending AbstractController.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like my old PR: #54726 ?

{
$request = $this->container->get('request_stack')->getCurrentRequest();
$form->handleRequest($request);

return $form->isSubmitted() && $form->isValid();
}

private function doRenderView(string $view, ?string $block, array $parameters, string $method): string
{
if (!$this->container->has('twig')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,44 @@ public function testSendEarlyHints()

$this->assertSame('</style.css>; rel="preload"; as="stylesheet",</script.js>; rel="preload"; as="script"', $response->headers->get('Link'));
}

public function testIsSubmittedFormValidReturnTrue()
{
$form = $this->createMock(FormInterface::class);
$form->expects($this->once())->method('handleRequest');
$form->method('isSubmitted')->willReturn(true);
$form->method('isValid')->willReturn(true);

$request = new Request();
$requestStack = new RequestStack();
$requestStack->push($request);

$container = new Container();
$container->set('request_stack', $requestStack);

$controller = $this->createController();
$controller->setContainer($container);

$this->assertTrue($controller->isSubmittedFormValid($form));
}

public function testIsSubmittedFormValidReturnfalse()
{
$form = $this->createMock(FormInterface::class);
$form->expects($this->once())->method('handleRequest');
$form->method('isSubmitted')->willReturn(true);
$form->method('isValid')->willReturn(false);

$request = new Request();
$requestStack = new RequestStack();
$requestStack->push($request);

$container = new Container();
$container->set('request_stack', $requestStack);

$controller = $this->createController();
$controller->setContainer($container);

$this->assertFalse($controller->isSubmittedFormValid($form));
}
}