Skip to content

Commit 29f610f

Browse files
committed
minor #15217 [Form][FrameworkBundle] Replace render() with new renderForm() in form documentation (MrYamous, javiereguiluz)
This PR was submitted for the 5.4 branch but it was merged into the 5.3 branch instead. Discussion ---------- [Form][FrameworkBundle] Replace render() with new renderForm() in form documentation This adds some missing changes to #15105 and fixes #14844. Commits ------- fdb7b67 Update code to the latest helper behavior 773e816 More changes for forms 4b259cc Replace render by new renderForm method in form documentation
2 parents a6e8143 + fdb7b67 commit 29f610f

File tree

7 files changed

+32
-25
lines changed

7 files changed

+32
-25
lines changed

components/form.rst

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ 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(),
177+
return $this->renderForm('product/new.html.twig', [
178+
'form' => $form,
179179
]);
180180
}
181181
}

form/direct_submit.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ control over when exactly your form is submitted and what data is passed to it::
2929
}
3030
}
3131

32-
return $this->render('task/new.html.twig', [
33-
'form' => $form->createView(),
32+
return $this->renderForm('task/new.html.twig', [
33+
'form' => $form,
3434
]);
3535
}
3636

form/dynamic_form_modification.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,9 @@ your application. Assume that you have a sport meetup creation controller::
534534
// ... save the meetup, redirect etc.
535535
}
536536

537-
return $this->render(
538-
'meetup/create.html.twig',
539-
['form' => $form->createView()]
540-
);
537+
return $this->renderForm('meetup/create.html.twig', [
538+
'form' => $form,
539+
]);
541540
}
542541

543542
// ...

form/form_collections.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ In your controller, you'll create a new form from the ``TaskType``::
164164
// ... do your form processing, like saving the Task and Tag entities
165165
}
166166

167-
return $this->render('task/new.html.twig', [
168-
'form' => $form->createView(),
167+
return $this->renderForm('task/new.html.twig', [
168+
'form' => $form,
169169
]);
170170
}
171171
}

form/form_customization.rst

Lines changed: 3 additions & 2 deletions
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' => $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

Lines changed: 14 additions & 13 deletions
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;
@@ -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+
286293
Then, use some :ref:`form helper functions <reference-form-twig-functions>` to
287294
render the form contents:
288295

@@ -405,8 +412,8 @@ written into the form object::
405412
return $this->redirectToRoute('task_success');
406413
}
407414

408-
return $this->render('task/new.html.twig', [
409-
'form' => $form->createView(),
415+
return $this->renderForm('task/new.html.twig', [
416+
'form' => $form,
410417
]);
411418
}
412419
}
@@ -437,12 +444,6 @@ possible paths:
437444
that prevents the user from being able to hit the "Refresh" button of
438445
their browser and re-post the data.
439446

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

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

0 commit comments

Comments
 (0)