From 4b259cc8e783c167c5f415e530a21353d03a5093 Mon Sep 17 00:00:00 2001 From: MrYamous Date: Sun, 14 Mar 2021 19:54:20 +0100 Subject: [PATCH 1/3] Replace render by new renderForm method in form documentation --- form/direct_submit.rst | 4 +--- form/dynamic_form_modification.rst | 5 +---- form/form_collections.rst | 4 +--- forms.rst | 13 +++++++------ 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/form/direct_submit.rst b/form/direct_submit.rst index 92dc09c5de5..2d42c8116ec 100644 --- a/form/direct_submit.rst +++ b/form/direct_submit.rst @@ -29,9 +29,7 @@ control over when exactly your form is submitted and what data is passed to it:: } } - return $this->render('task/new.html.twig', [ - 'form' => $form->createView(), - ]); + return $this->renderForm('task/new.html.twig', $form); } The list of fields submitted with the ``submit()`` method must be the same as diff --git a/form/dynamic_form_modification.rst b/form/dynamic_form_modification.rst index 7c52e5f3abd..e0b25e92190 100644 --- a/form/dynamic_form_modification.rst +++ b/form/dynamic_form_modification.rst @@ -534,10 +534,7 @@ your application. Assume that you have a sport meetup creation controller:: // ... save the meetup, redirect etc. } - return $this->render( - 'meetup/create.html.twig', - ['form' => $form->createView()] - ); + return $this->renderForm('meetup/create.html.twig', $form); } // ... diff --git a/form/form_collections.rst b/form/form_collections.rst index e35e1d8be67..3d7393ac9e4 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -164,9 +164,7 @@ In your controller, you'll create a new form from the ``TaskType``:: // ... do your form processing, like saving the Task and Tag entities } - return $this->render('task/new.html.twig', [ - 'form' => $form->createView(), - ]); + return $this->renderForm('task/new.html.twig', $form); } } diff --git a/forms.rst b/forms.rst index 3814caa0646..45d2c79b1e1 100644 --- a/forms.rst +++ b/forms.rst @@ -277,12 +277,15 @@ to build another object with the visual representation of the form:: $form = $this->createForm(TaskType::class, $task); - return $this->render('task/new.html.twig', [ - 'form' => $form->createView(), - ]); + return $this->renderForm('task/new.html.twig', $form); } } +.. versionadded:: 5.3 + + The ``renderForm`` method was introduced in Symfony 5.3, allowing to + return 422 HTTP status code if an invalid form is submitted. + Then, use some :ref:`form helper functions ` to render the form contents: @@ -405,9 +408,7 @@ written into the form object:: return $this->redirectToRoute('task_success'); } - return $this->render('task/new.html.twig', [ - 'form' => $form->createView(), - ]); + return $this->renderForm('task/new.html.twig', $form); } } From 773e81648adc917f5d97f0cc332c5deb5355b40c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 9 Apr 2021 17:14:19 +0200 Subject: [PATCH 2/3] More changes for forms --- components/form.rst | 6 ++++++ controller/upload_file.rst | 4 +--- form/form_customization.rst | 5 +++-- forms.rst | 18 +++++++----------- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/components/form.rst b/components/form.rst index 2b387ef326e..64551b72041 100644 --- a/components/form.rst +++ b/components/form.rst @@ -647,6 +647,12 @@ method: } } +.. caution:: + + The form's ``createView()`` method should be called *after* ``handleRequest()`` is + called. Otherwise, when using :doc:`form events `, changes done + in the ``*_SUBMIT`` events won't be applied to the view (like validation errors). + This defines a common form "workflow", which contains 3 different possibilities: 1) On the initial GET request (i.e. when the user "surfs" to your page), diff --git a/controller/upload_file.rst b/controller/upload_file.rst index edd17ed50dc..e908bf6e881 100644 --- a/controller/upload_file.rst +++ b/controller/upload_file.rst @@ -174,9 +174,7 @@ Finally, you need to update the code of the controller that handles the form:: return $this->redirectToRoute('app_product_list'); } - return $this->render('product/new.html.twig', [ - 'form' => $form->createView(), - ]); + return $this->renderForm('product/new.html.twig', $form); } } diff --git a/form/form_customization.rst b/form/form_customization.rst index 69d0a97e61d..bcc48070e63 100644 --- a/form/form_customization.rst +++ b/form/form_customization.rst @@ -20,8 +20,9 @@ enough to render an entire form, including all its fields and error messages: .. code-block:: twig - {# form is a variable passed from the controller and created - by calling to the $form->createView() method #} + {# form is a variable passed from the controller via either + $this->renderForm('...', $form) + or $this->render('...', ['form' => $form->createView()]) #} {{ form(form) }} The next step is to use the :ref:`form_start() `, diff --git a/forms.rst b/forms.rst index 45d2c79b1e1..c1fc9c10bf3 100644 --- a/forms.rst +++ b/forms.rst @@ -255,9 +255,7 @@ the ``data_class`` option by adding the following to your form type class:: Rendering Forms --------------- -Now that the form has been created, the next step is to render it. Instead of -passing the entire form object to the template, use the ``createView()`` method -to build another object with the visual representation of the form:: +Now that the form has been created, the next step is to render it:: // src/Controller/TaskController.php namespace App\Controller; @@ -281,10 +279,14 @@ to build another object with the visual representation of the form:: } } +In versions prior to Symfony 5.3, controllers used the method +``$this->render('...', ['form' => $form->createView()])`` to render the form. +The ``renderForm()`` method abstracts this logic and it also sets the 422 HTTP +status code in the response automatically when the submitted form is not valid. + .. versionadded:: 5.3 - The ``renderForm`` method was introduced in Symfony 5.3, allowing to - return 422 HTTP status code if an invalid form is submitted. + The ``renderForm()`` method was introduced in Symfony 5.3. Then, use some :ref:`form helper functions ` to render the form contents: @@ -438,12 +440,6 @@ possible paths: that prevents the user from being able to hit the "Refresh" button of their browser and re-post the data. -.. caution:: - - The ``createView()`` method should be called *after* ``handleRequest()`` is - called. Otherwise, when using :doc:`form events `, changes done - in the ``*_SUBMIT`` events won't be applied to the view (like validation errors). - .. seealso:: If you need more control over exactly when your form is submitted or which From fdb7b6747ec0e39d51b15537d508ab0bdcb7ab16 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 23 Sep 2021 11:26:05 +0200 Subject: [PATCH 3/3] Update code to the latest helper behavior --- controller/upload_file.rst | 4 +++- form/direct_submit.rst | 4 +++- form/dynamic_form_modification.rst | 4 +++- form/form_collections.rst | 4 +++- form/form_customization.rst | 2 +- forms.rst | 8 ++++++-- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/controller/upload_file.rst b/controller/upload_file.rst index e908bf6e881..2abf1dc34c0 100644 --- a/controller/upload_file.rst +++ b/controller/upload_file.rst @@ -174,7 +174,9 @@ Finally, you need to update the code of the controller that handles the form:: return $this->redirectToRoute('app_product_list'); } - return $this->renderForm('product/new.html.twig', $form); + return $this->renderForm('product/new.html.twig', [ + 'form' => $form, + ]); } } diff --git a/form/direct_submit.rst b/form/direct_submit.rst index 2d42c8116ec..a7c623dad19 100644 --- a/form/direct_submit.rst +++ b/form/direct_submit.rst @@ -29,7 +29,9 @@ control over when exactly your form is submitted and what data is passed to it:: } } - return $this->renderForm('task/new.html.twig', $form); + return $this->renderForm('task/new.html.twig', [ + 'form' => $form, + ]); } The list of fields submitted with the ``submit()`` method must be the same as diff --git a/form/dynamic_form_modification.rst b/form/dynamic_form_modification.rst index e0b25e92190..afc1b48236a 100644 --- a/form/dynamic_form_modification.rst +++ b/form/dynamic_form_modification.rst @@ -534,7 +534,9 @@ your application. Assume that you have a sport meetup creation controller:: // ... save the meetup, redirect etc. } - return $this->renderForm('meetup/create.html.twig', $form); + return $this->renderForm('meetup/create.html.twig', [ + 'form' => $form, + ]); } // ... diff --git a/form/form_collections.rst b/form/form_collections.rst index 3d7393ac9e4..c58bf996235 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -164,7 +164,9 @@ In your controller, you'll create a new form from the ``TaskType``:: // ... do your form processing, like saving the Task and Tag entities } - return $this->renderForm('task/new.html.twig', $form); + return $this->renderForm('task/new.html.twig', [ + 'form' => $form, + ]); } } diff --git a/form/form_customization.rst b/form/form_customization.rst index bcc48070e63..98f8d433ce8 100644 --- a/form/form_customization.rst +++ b/form/form_customization.rst @@ -21,7 +21,7 @@ enough to render an entire form, including all its fields and error messages: .. code-block:: twig {# form is a variable passed from the controller via either - $this->renderForm('...', $form) + $this->renderForm('...', ['form' => $form]) or $this->render('...', ['form' => $form->createView()]) #} {{ form(form) }} diff --git a/forms.rst b/forms.rst index c1fc9c10bf3..74fef0b6600 100644 --- a/forms.rst +++ b/forms.rst @@ -275,7 +275,9 @@ Now that the form has been created, the next step is to render it:: $form = $this->createForm(TaskType::class, $task); - return $this->renderForm('task/new.html.twig', $form); + return $this->renderForm('task/new.html.twig', [ + 'form' => $form, + ]); } } @@ -410,7 +412,9 @@ written into the form object:: return $this->redirectToRoute('task_success'); } - return $this->renderForm('task/new.html.twig', $form); + return $this->renderForm('task/new.html.twig', [ + 'form' => $form, + ]); } }