Skip to content

Commit d0a40c2

Browse files
committed
feature #16948 [Form] renderForm() method is deprecated (mdoutreluingne)
This PR was merged into the 6.2 branch. Discussion ---------- [Form] `renderForm()` method is deprecated Fixes #16942 Commits ------- 0c39faf Deprecate `renderForm()`
2 parents 2870f49 + 0c39faf commit d0a40c2

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

form/form_customization.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ 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 via either
24-
$this->renderForm('...', ['form' => $form])
23+
{# form is a variable passed from the controller via
24+
$this->render('...', ['form' => $form])
2525
or $this->render('...', ['form' => $form->createView()]) #}
2626
{{ form(form) }}
2727

forms.rst

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,21 @@ Now that the form has been created, the next step is to render it::
275275

276276
$form = $this->createForm(TaskType::class, $task);
277277

278-
return $this->renderForm('task/new.html.twig', [
278+
return $this->render('task/new.html.twig', [
279279
'form' => $form,
280280
]);
281281
}
282282
}
283283

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()``.
288293

289294
Then, use some :ref:`form helper functions <reference-form-twig-functions>` to
290295
render the form contents:
@@ -404,7 +409,7 @@ written into the form object::
404409
return $this->redirectToRoute('task_success');
405410
}
406411

407-
return $this->renderForm('task/new.html.twig', [
412+
return $this->render('task/new.html.twig', [
408413
'form' => $form,
409414
]);
410415
}
@@ -422,7 +427,12 @@ possible paths:
422427
``task`` and ``dueDate`` properties of the ``$task`` object. Then this object
423428
is validated (validation is explained in the next section). If it is invalid,
424429
: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`_;
426436

427437
#. When the user submits the form with valid data, the submitted data is again
428438
written into the form, but this time :method:`Symfony\\Component\\Form\\FormInterface::isValid`
@@ -1039,3 +1049,5 @@ Misc.:
10391049

10401050
.. _`Symfony Forms screencast series`: https://symfonycasts.com/screencast/symfony-forms
10411051
.. _`MakerBundle`: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html
1052+
.. _`HTTP 422 Unprocessable Content`: https://www.rfc-editor.org/rfc/rfc9110.html#name-422-unprocessable-content
1053+
.. _`Symfony UX Turbo`: https://ux.symfony.com/turbo

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy