Skip to content

Commit 773e816

Browse files
committed
More changes for forms
1 parent 4b259cc commit 773e816

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

components/form.rst

+6
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,12 @@ method:
647647
}
648648
}
649649
650+
.. caution::
651+
652+
The form's ``createView()`` method should be called *after* ``handleRequest()`` is
653+
called. Otherwise, when using :doc:`form events </form/events>`, changes done
654+
in the ``*_SUBMIT`` events won't be applied to the view (like validation errors).
655+
650656
This defines a common form "workflow", which contains 3 different possibilities:
651657

652658
1) On the initial GET request (i.e. when the user "surfs" to your page),

controller/upload_file.rst

+1-3
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ Finally, you need to update the code of the controller that handles the form::
174174
return $this->redirectToRoute('app_product_list');
175175
}
176176

177-
return $this->render('product/new.html.twig', [
178-
'form' => $form->createView(),
179-
]);
177+
return $this->renderForm('product/new.html.twig', $form);
180178
}
181179
}
182180

form/form_customization.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ enough to render an entire form, including all its fields and error messages:
2020

2121
.. code-block:: twig
2222
23-
{# form is a variable passed from the controller and created
24-
by calling to the $form->createView() method #}
23+
{# form is a variable passed from the controller via either
24+
$this->renderForm('...', $form)
25+
or $this->render('...', ['form' => $form->createView()]) #}
2526
{{ form(form) }}
2627
2728
The next step is to use the :ref:`form_start() <reference-forms-twig-start>`,

forms.rst

+7-11
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ the ``data_class`` option by adding the following to your form type class::
255255
Rendering Forms
256256
---------------
257257

258-
Now that the form has been created, the next step is to render it. Instead of
259-
passing the entire form object to the template, use the ``createView()`` method
260-
to build another object with the visual representation of the form::
258+
Now that the form has been created, the next step is to render it::
261259

262260
// src/Controller/TaskController.php
263261
namespace App\Controller;
@@ -281,10 +279,14 @@ to build another object with the visual representation of the form::
281279
}
282280
}
283281

282+
In versions prior to Symfony 5.3, controllers used the method
283+
``$this->render('...', ['form' => $form->createView()])`` to render the form.
284+
The ``renderForm()`` method abstracts this logic and it also sets the 422 HTTP
285+
status code in the response automatically when the submitted form is not valid.
286+
284287
.. versionadded:: 5.3
285288

286-
The ``renderForm`` method was introduced in Symfony 5.3, allowing to
287-
return 422 HTTP status code if an invalid form is submitted.
289+
The ``renderForm()`` method was introduced in Symfony 5.3.
288290

289291
Then, use some :ref:`form helper functions <reference-form-twig-functions>` to
290292
render the form contents:
@@ -438,12 +440,6 @@ possible paths:
438440
that prevents the user from being able to hit the "Refresh" button of
439441
their browser and re-post the data.
440442

441-
.. caution::
442-
443-
The ``createView()`` method should be called *after* ``handleRequest()`` is
444-
called. Otherwise, when using :doc:`form events </form/events>`, changes done
445-
in the ``*_SUBMIT`` events won't be applied to the view (like validation errors).
446-
447443
.. seealso::
448444

449445
If you need more control over exactly when your form is submitted or which

0 commit comments

Comments
 (0)