Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[Form][FrameworkBundle] Replace render() with new renderForm() in form documentation#15217
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
javiereguiluz commentedApr 14, 2021
Don't merge yet. Let's wait forsymfony/symfony#40799 to decide the best way of solving this. |
… helper (dunglas)This PR was squashed before being merged into the 5.3-dev branch.Discussion----------[FrameworkBundle] Add AbstractController::handleForm() helper| Q | A| ------------- | ---| Branch? | 5.x| Bug fix? | no| New feature? | yes <!-- please update src/**/CHANGELOG.md files -->| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tickets | n/a| License | MIT| Doc PR |symfony/symfony-docs#15217Some libraries such as Turbo require to strictly follow the HTTP specification (and especially to use proper status codes) to deal with forms.In#39843, I introduced a new `renderForm()` helper for this purpose. But I'm not very satisfied by it. The current approach has several problems:1. It calls `$form->isValid()` two times, which may hurt performance2. It sets the proper status code in case of validation error (422), but not for the redirection when the entity is created or updated (306). The user must do this manually (and so be aware of these HTTP subtleties).3. It hides the verbosity of the Form component a bit, but I'm a sure that we can reduce it moreThis PR proposes an alternative helper, `handleForm()`, which handles automatically 80% of the use cases, provide an extension point for the other 20%, and can also serve as a quick example for users to handle form in a custom way (by control-clicking on the function to see the code and copy/paste/adapt it).* if the form is not submitted, the Twig template passed in $view is rendered and a 200 HTTP status code is set* if the form is submitted but invalid, the Twig template passed in $view is rendered and 422 HTTP status code is set* 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 $redirectUrlBefore (standard case):```php #[Route('/{id}/edit', name: 'conference_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Conference $conference): Response { $form = $this->createForm(ConferenceType::class, $conference); $form->handleRequest($request); $submitted = $form->isSubmitted(); $valid = $submitted && $form->isValid(); if ($valid) { $this->getDoctrine()->getManager()->flush(); return $this->redirectToRoute('conference_index', [], Response::HTTP_SEE_OTHER); } $response = $this->render('conference/edit.html.twig', [ 'conference' => $conference, 'form' => $form->createView(), ]); if ($submitted && !$valid) { $response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); } return $response; }```With the new helper:```php #[Route('/{id}/edit', name: 'conference_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Conference $conference): Response { $form = $this->createForm(ConferenceType::class, $conference); return $this->handleForm( $request, $form, view: 'conference/edit.html.twig', redirectUrl: $this->generateUrl('conference_index') ); }```Before (more advanced use case):```php #[Route('/{id}/edit', name: 'conference_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Conference $conference, HubInterface $hub): Response { $form = $this->createForm(ConferenceType::class, $conference); $form->handleRequest($request); $submitted = $form->isSubmitted(); $valid = $submitted && $form->isValid(); if ($valid) { $this->getDoctrine()->getManager()->flush(); $hub->publish( new Update( 'conference:'.$conference->getId(), $this->renderView('conference/edit.stream.html.twig', ['conference' => $conference]) ) ); return $this->redirectToRoute('conference_index', [], Response::HTTP_SEE_OTHER); } $response = $this->render('conference/edit.html.twig', [ 'conference' => $conference, 'form' => $form->createView(), ]); if ($submitted && !$valid) { $response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); } return $response; }```With the new helper (more advanced case):```php #[Route('/{id}/edit', name: 'conference_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Conference $conference, HubInterface $hub): Response { $form = $this->createForm(ConferenceType::class, $conference); $response = $this->handleForm( $request, $form, view: 'conference/edit.html.twig', redirectUrl: $this->generateUrl('conference_index') ); if ($response->isRedirection()) { $hub->publish( new Update( 'conference:' . $conference->getId(), $this->renderView('conference/edit.stream.html.twig', ['conference' => $conference]) ) ); } return $response; }```This also works without named parameters. I also considered passing a callback to be executed on success, but I'm happier with the current solution.WDYT?TODO:* [x] update testsCommits-------5228546 [FrameworkBundle] Add AbstractController::handleForm() helper
… helper (dunglas)This PR was squashed before being merged into the 5.3-dev branch.Discussion----------[FrameworkBundle] Add AbstractController::handleForm() helper| Q | A| ------------- | ---| Branch? | 5.x| Bug fix? | no| New feature? | yes <!-- please update src/**/CHANGELOG.md files -->| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tickets | n/a| License | MIT| Doc PR |symfony/symfony-docs#15217Some libraries such as Turbo require to strictly follow the HTTP specification (and especially to use proper status codes) to deal with forms.Insymfony/symfony#39843, I introduced a new `renderForm()` helper for this purpose. But I'm not very satisfied by it. The current approach has several problems:1. It calls `$form->isValid()` two times, which may hurt performance2. It sets the proper status code in case of validation error (422), but not for the redirection when the entity is created or updated (306). The user must do this manually (and so be aware of these HTTP subtleties).3. It hides the verbosity of the Form component a bit, but I'm a sure that we can reduce it moreThis PR proposes an alternative helper, `handleForm()`, which handles automatically 80% of the use cases, provide an extension point for the other 20%, and can also serve as a quick example for users to handle form in a custom way (by control-clicking on the function to see the code and copy/paste/adapt it).* if the form is not submitted, the Twig template passed in $view is rendered and a 200 HTTP status code is set* if the form is submitted but invalid, the Twig template passed in $view is rendered and 422 HTTP status code is set* 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 $redirectUrlBefore (standard case):```php #[Route('/{id}/edit', name: 'conference_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Conference $conference): Response { $form = $this->createForm(ConferenceType::class, $conference); $form->handleRequest($request); $submitted = $form->isSubmitted(); $valid = $submitted && $form->isValid(); if ($valid) { $this->getDoctrine()->getManager()->flush(); return $this->redirectToRoute('conference_index', [], Response::HTTP_SEE_OTHER); } $response = $this->render('conference/edit.html.twig', [ 'conference' => $conference, 'form' => $form->createView(), ]); if ($submitted && !$valid) { $response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); } return $response; }```With the new helper:```php #[Route('/{id}/edit', name: 'conference_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Conference $conference): Response { $form = $this->createForm(ConferenceType::class, $conference); return $this->handleForm( $request, $form, view: 'conference/edit.html.twig', redirectUrl: $this->generateUrl('conference_index') ); }```Before (more advanced use case):```php #[Route('/{id}/edit', name: 'conference_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Conference $conference, HubInterface $hub): Response { $form = $this->createForm(ConferenceType::class, $conference); $form->handleRequest($request); $submitted = $form->isSubmitted(); $valid = $submitted && $form->isValid(); if ($valid) { $this->getDoctrine()->getManager()->flush(); $hub->publish( new Update( 'conference:'.$conference->getId(), $this->renderView('conference/edit.stream.html.twig', ['conference' => $conference]) ) ); return $this->redirectToRoute('conference_index', [], Response::HTTP_SEE_OTHER); } $response = $this->render('conference/edit.html.twig', [ 'conference' => $conference, 'form' => $form->createView(), ]); if ($submitted && !$valid) { $response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); } return $response; }```With the new helper (more advanced case):```php #[Route('/{id}/edit', name: 'conference_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Conference $conference, HubInterface $hub): Response { $form = $this->createForm(ConferenceType::class, $conference); $response = $this->handleForm( $request, $form, view: 'conference/edit.html.twig', redirectUrl: $this->generateUrl('conference_index') ); if ($response->isRedirection()) { $hub->publish( new Update( 'conference:' . $conference->getId(), $this->renderView('conference/edit.stream.html.twig', ['conference' => $conference]) ) ); } return $response; }```This also works without named parameters. I also considered passing a callback to be executed on success, but I'm happier with the current solution.WDYT?TODO:* [x] update testsCommits-------5228546066 [FrameworkBundle] Add AbstractController::handleForm() helper
javiereguiluz commentedApr 20, 2021
After the latest changes made on Traditional form handling: #[Route('/{id}/edit', name:'conference_edit', methods: ['GET','POST'])]publicfunctionedit(Request$request,Conference$conference):Response{$form =$this->createForm(ConferenceType::class,$conference);$form->handleRequest($request);if ($form->isSubmitted() &&$form->isValid()) {$this->getDoctrine()->getManager()->flush();return$this->redirectToRoute('conference_index', [], Response::HTTP_SEE_OTHER); }return$this->render('conference/edit.html.twig', ['conference' =>$conference,'form' =>$form->createView(), ]);} Form handling with #[Route('/{id}/edit', name:'conference_edit', methods: ['GET','POST'])]publicfunctionedit(Request$request,Conference$conference):Response{return$this->handleForm($request,$this->createForm(ConferenceType::class,$conference),function (FormInterface$form)use ($conference) {$this->getDoctrine()->getManager()->flush();return$this->redirectToRoute('conference_show', ['id' =>$conference->getId()], Response::HTTP_SEE_OTHER ); },function (FormInterface$form) {return$this->render('conference/edit.html.twig', ['form' =>$form->createView(), ]); } );} However, since this |
nicolas-grekas commentedMay 12, 2021
renderForm()is back, so that this PR might be good to merge now :) |
nicolas-grekas commentedMay 12, 2021
Should be updated forsymfony/symfony#41190 |
Uh oh!
There was an error while loading.Please reload this page.
This adds some missing changes to#15105 andfixes#14844.