@@ -255,9 +255,7 @@ the ``data_class`` option by adding the following to your form type class::
255255Rendering 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;
@@ -277,12 +275,21 @@ to build another object with the visual representation of the form::
277275
278276 $form = $this->createForm(TaskType::class, $task);
279277
280- return $this->render ('task/new.html.twig', [
281- 'form' => $form->createView() ,
278+ return $this->renderForm ('task/new.html.twig', [
279+ 'form' => $form,
282280 ]);
283281 }
284282 }
285283
284+ In versions prior to Symfony 5.3, controllers used the method
285+ ``$this->render('...', ['form' => $form->createView()]) `` to render the form.
286+ The ``renderForm() `` method abstracts this logic and it also sets the 422 HTTP
287+ status code in the response automatically when the submitted form is not valid.
288+
289+ .. versionadded :: 5.3
290+
291+ The ``renderForm() `` method was introduced in Symfony 5.3.
292+
286293Then, use some :ref: `form helper functions <reference-form-twig-functions >` to
287294render the form contents:
288295
@@ -401,8 +408,8 @@ written into the form object::
401408 return $this->redirectToRoute('task_success');
402409 }
403410
404- return $this->render ('task/new.html.twig', [
405- 'form' => $form->createView() ,
411+ return $this->renderForm ('task/new.html.twig', [
412+ 'form' => $form,
406413 ]);
407414 }
408415 }
@@ -433,12 +440,6 @@ possible paths:
433440 that prevents the user from being able to hit the "Refresh" button of
434441 their browser and re-post the data.
435442
436- .. caution ::
437-
438- The ``createView() `` method should be called *after * ``handleRequest() `` is
439- called. Otherwise, when using :doc: `form events </form/events >`, changes done
440- in the ``*_SUBMIT `` events won't be applied to the view (like validation errors).
441-
442443.. seealso ::
443444
444445 If you need more control over exactly when your form is submitted or which
0 commit comments