Skip to content

Commit 2a83b57

Browse files
committed
new iteration
1 parent c676616 commit 2a83b57

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -297,29 +297,28 @@ protected function stream(string $view, array $parameters = [], StreamedResponse
297297
* * if the form is submitted but invalid, the Twig template passed in $view is rendered and 422 HTTP status code is set
298298
* * if the form is submitted and valid, the entity is saved (only if it is managed by Doctrine ORM), a 306 HTTP status code is set and the Location HTTP header is set to the value of $redirectUrl
299299
*/
300-
public function handleForm(Request $request, FormInterface $form, string $view, ?string $redirectUrl = null, ?callable $onSuccess = null, array $viewParameters = [], ?Response $response = null): Response
300+
301+
/**
302+
* Handles a form.
303+
*
304+
* * if the form is not submitted, $render is called
305+
* * if the form is submitted but invalid, $render is called and a 422 HTTP status code is set if the current status isn't in the 4XX range
306+
* * if the form is submitted and valid, $onSuccess is called, usually this method saves the data and returns a 306 HTTP redirection
307+
*
308+
* @param callable(FormInterface): Response $onSuccess
309+
* @param callable(FormInterface): Response $render
310+
*/
311+
public function handleForm(Request $request, FormInterface $form, callable $onSuccess, callable $render): Response
301312
{
302313
$form->handleRequest($request);
303314

304315
$submitted = $form->isSubmitted();
305-
$valid = $submitted && $form->isValid();
306-
307-
if ($valid) {
308-
if (null !== $onSuccess) {
309-
return $onSuccess();
310-
}
311-
312-
$data = $form->getData();
313-
if ($manager = $this->getDoctrine()->getManagerForClass(get_class($data))) {
314-
$manager->persist($data);
315-
$manager->flush();
316-
}
317-
318-
return $this->redirect($redirectUrl, Response::HTTP_SEE_OTHER);
316+
if ($submitted && $form->isValid()) {
317+
return $onSuccess($form);
319318
}
320319

321-
$response = $this->render($view, ['form' => $form->createView()] + $viewParameters, $response);
322-
if ($submitted) {
320+
$response = $render($form);
321+
if ($submitted && ($code = $response->getStatusCode()) < 400 || $code >= 500) {
323322
$response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
324323
}
325324

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy