@@ -275,16 +275,21 @@ Now that the form has been created, the next step is to render it::
275
275
276
276
$form = $this->createForm(TaskType::class, $task);
277
277
278
- return $this->renderForm ('task/new.html.twig', [
278
+ return $this->render ('task/new.html.twig', [
279
279
'form' => $form,
280
280
]);
281
281
}
282
282
}
283
283
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.
284
+ Internally, the ``render() `` method calls ``$form->createView() `` to
285
+ transform the form into a *form view * instance.
286
+
287
+ .. deprecated :: 6.2
288
+
289
+ Prior to Symfony 6.2, you had to use ``$this->render(..., ['form' => $form->createView()]) ``
290
+ or the ``renderForm() `` method to render to form. The ``renderForm() ``
291
+ method is deprecated in favor of directly passing the ``FormInterface ``
292
+ instance to ``render() ``.
288
293
289
294
Then, use some :ref: `form helper functions <reference-form-twig-functions >` to
290
295
render the form contents:
@@ -404,7 +409,7 @@ written into the form object::
404
409
return $this->redirectToRoute('task_success');
405
410
}
406
411
407
- return $this->renderForm ('task/new.html.twig', [
412
+ return $this->render ('task/new.html.twig', [
408
413
'form' => $form,
409
414
]);
410
415
}
@@ -422,7 +427,12 @@ possible paths:
422
427
``task `` and ``dueDate `` properties of the ``$task `` object. Then this object
423
428
is validated (validation is explained in the next section). If it is invalid,
424
429
:method: `Symfony\\ Component\\ Form\\ FormInterface::isValid ` returns
425
- ``false `` and the form is rendered again, but now with validation errors;
430
+ ``false `` and the form is rendered again, but now with validation errors.
431
+
432
+ By passing ``$form `` to the ``render() `` method (instead of
433
+ ``$form->createView() ``), the response code is automatically set to
434
+ `HTTP 422 Unprocessable Content `_. This ensures compatibility with tools
435
+ relying on the HTTP specification, like `Symfony UX Turbo `_;
426
436
427
437
#. When the user submits the form with valid data, the submitted data is again
428
438
written into the form, but this time :method: `Symfony\\ Component\\ Form\\ FormInterface::isValid `
@@ -1070,3 +1080,5 @@ Misc.:
1070
1080
1071
1081
.. _`Symfony Forms screencast series` : https://symfonycasts.com/screencast/symfony-forms
1072
1082
.. _`MakerBundle` : https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html
1083
+ .. _`HTTP 422 Unprocessable Content` : https://www.rfc-editor.org/rfc/rfc9110.html#name-422-unprocessable-content
1084
+ .. _`Symfony UX Turbo` : https://ux.symfony.com/turbo
0 commit comments