Skip to content

Commit b23fe89

Browse files
committed
new iteration
1 parent c676616 commit b23fe89

File tree

1 file changed

+12
-37
lines changed

1 file changed

+12
-37
lines changed

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

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -291,51 +291,26 @@ protected function stream(string $view, array $parameters = [], StreamedResponse
291291
}
292292

293293
/**
294-
* Handles a form:
294+
* Handles a form.
295295
*
296-
* * if the form is not submitted, the Twig template passed in $view is rendered and a 200 HTTP status code is set
297-
* * if the form is submitted but invalid, the Twig template passed in $view is rendered and 422 HTTP status code is set
298-
* * 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
296+
* * if the form is not submitted, $render is called
297+
* * if the form is submitted but invalid, $render is called and a 422 HTTP status code is set if the current status hasn't been customized
298+
* * if the form is submitted and valid, $onSuccess is called, usually this method saves the data and returns a 306 HTTP redirection
299+
*
300+
* @param callable(FormInterface): Response $onSuccess
301+
* @param callable(FormInterface): Response $render
299302
*/
300-
public function handleForm(Request $request, FormInterface $form, string $view, ?string $redirectUrl = null, ?callable $onSuccess = null, array $viewParameters = [], ?Response $response = null): Response
303+
public function handleForm(Request $request, FormInterface $form, callable $onSuccess, callable $render): Response
301304
{
302305
$form->handleRequest($request);
303306

304307
$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);
319-
}
320-
321-
$response = $this->render($view, ['form' => $form->createView()] + $viewParameters, $response);
322-
if ($submitted) {
323-
$response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
308+
if ($submitted && $form->isValid()) {
309+
return $onSuccess($form);
324310
}
325311

326-
return $response;
327-
}
328-
329-
/**
330-
* Renders a form.
331-
*
332-
* The FormView instance is passed to the template in a variable named "form".
333-
* If the form is invalid, a 422 status code is returned.
334-
*/
335-
public function renderForm(string $view, FormInterface $form, array $parameters = [], Response $response = null): Response
336-
{
337-
$response = $this->render($view, ['form' => $form->createView()] + $parameters, $response);
338-
if ($form->isSubmitted() && !$form->isValid()) {
312+
$response = $render($form);
313+
if ($submitted && 200 === $response->getStatusCode()) {
339314
$response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
340315
}
341316

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