Skip to content

[Form] Mention a possible way of reusing the same form #14719

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

Merged
merged 1 commit into from
Dec 21, 2020

Conversation

javiereguiluz
Copy link
Member

This fixes #13977 by implementing the idea number 2 of this comment: #13977 (comment)

@javiereguiluz javiereguiluz added this to the 4.4 milestone Dec 18, 2020
@carsonbot carsonbot changed the title Mention a possible way of reusing the same form [Form] Mention a possible way of reusing the same form Dec 18, 2020
Copy link
Contributor

@noniagriconomie noniagriconomie left a comment

Choose a reason for hiding this comment

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

👍 this was asked recently on Slack

@dvapelnik
Copy link
Contributor

I have some comments about this tip
In case when we have a controller that should show and handle the form we cannot simply use it as an embedded controller.
We have controller

class FooFormController extends AbstractController {
    public function __invoke(Request $request): Response {
        $dto = new FooDto();
        $form = $this->createForm(FooFormType::class, $dto)->handle($request);
        
        if ($form->isSubmitted() && $form->isValid()) {
            // some form handling logic
            // redirecting to this route
            return $this->redirectToRoute('foo_form_route');
        }

        return $this->render('foo.form.html.twig', ['form' => $form->createView()]);
    }
}

This controller suggests that foo.form.html.twig should extend some layout template. In form should use the current route for form action.

When we want to use this controller as an embedded we should

  • Set up action for form handling. The form will use the current route for action in an embedded state if we didn't. The route can be handled by this controller. Or see next.
  • Use master request for handling - not a subrequest bc it has not any form data from POST-request. So form should not be handled. And see next.
  • Do not extend some layout template. And see next.
  • Do not return redirect. The controller returns 302th redirect in Twig if we didn't. Should be caught the error: An exception has been thrown during the rendering of a template ("Error when rendering "http://example.com/foo-form" (Status code is 302).").. But in this case (without redirecting) there is the ability to re-posting data again on page refreshing.

There is one more fact about form handling in an embedded controller: form handling happens in the template rendering process and this should keep in mind.

So form controller embedding is not a simple way to form reusing.

I have a question: what is the best way to render and handle many various forms on one page as blocks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants