Skip to content

Commit e9807dd

Browse files
committed
Putting a bit more stuff in code, and a bit less stuff about Doctrine
Also removed Final Thoughts
1 parent b36f3e4 commit e9807dd

File tree

1 file changed

+15
-56
lines changed

1 file changed

+15
-56
lines changed

forms.rst

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ it into a format that's suitable for being rendered in an HTML form.
210210
Handling Form Submissions
211211
~~~~~~~~~~~~~~~~~~~~~~~~~
212212

213-
The second job of a form is to translate user-submitted data back to the
213+
By default, the form will submit a POST request back to the same controller that
214+
renders it.
215+
216+
Here, the second job of a form is to translate user-submitted data back to the
214217
properties of an object. To make this happen, the submitted data from the
215218
user must be written into the Form object. Add the following functionality to
216219
your controller::
@@ -231,9 +234,16 @@ your controller::
231234

232235
$form->handleRequest($request);
233236

234-
if ($form->isSubmitted() && $form->isValid()) {
235-
// the $task object will now contain the submitted data!
237+
if ($form->isSubmitted() && $form->()) {
238+
// $form->getData() holds the submitted values
239+
// but, the original `$task` variable has also been updated
240+
$task = $form->getData();
241+
236242
// ... perform some action, such as saving the task to the database
243+
// for example, if Task is a Doctrine entity, save it!
244+
// $em = $this->getDoctrine()->getManager();
245+
// $em->persist($task);
246+
// $em->flush();
237247

238248
return $this->redirectToRoute('task_success');
239249
}
@@ -437,8 +447,8 @@ the common form fields and data types you'll encounter:
437447

438448
.. include:: /reference/forms/types/map.rst.inc
439449

440-
You can also create your own custom field types. This topic is covered in
441-
the ":doc:`/form/create_custom_field_type`" article of the cookbook.
450+
You can also create your own custom field types. See
451+
:doc:`/form/create_custom_field_type` for info.
442452

443453
.. index::
444454
single: Forms; Field type options
@@ -690,57 +700,6 @@ the choice is ultimately up to you.
690700

691701
$form->get('dueDate')->setData(new \DateTime());
692702

693-
.. index::
694-
pair: Forms; Doctrine
695-
696-
Forms and Doctrine
697-
------------------
698-
699-
The goal of a form is to translate data from an object (e.g. ``Task``) to an
700-
HTML form and then translate user-submitted data back to the original object. As
701-
such, the topic of persisting the ``Task`` object to the database is entirely
702-
unrelated to the topic of forms. But, if you've configured the ``Task`` class
703-
to be persisted via Doctrine (i.e. you've added
704-
:ref:`mapping metadata <book-doctrine-adding-mapping>` for it), then persisting
705-
it after a form submission can be done when the form is valid::
706-
707-
if ($form->isValid()) {
708-
$em = $this->getDoctrine()->getManager();
709-
$em->persist($task);
710-
$em->flush();
711-
712-
return $this->redirectToRoute('task_success');
713-
}
714-
715-
If, for some reason, you don't have access to your original ``$task`` object,
716-
you can fetch it from the form::
717-
718-
$task = $form->getData();
719-
720-
For more information, see the :doc:`Doctrine ORM chapter </doctrine>`.
721-
722-
The key thing to understand is that when the form is submitted, the submitted
723-
data is transferred to the underlying object immediately. If you want to
724-
persist that data, you simply need to persist the object itself (which already
725-
contains the submitted data).
726-
727-
Final Thoughts
728-
--------------
729-
730-
You now know all of the building blocks necessary to build complex and
731-
functional forms for your application. When building forms, keep in mind that
732-
the first goal of a form is to translate data from an object (``Task``) to an
733-
HTML form so that the user can modify that data. The second goal of a form is to
734-
take the data submitted by the user and to re-apply it to the object.
735-
736-
There's still much more to learn about the powerful world of forms, such as
737-
how to handle :doc:`file uploads </controller/upload_file>` or how to
738-
create a form where a dynamic number of sub-forms can be added (e.g. a todo
739-
list where you can keep adding more fields via JavaScript before submitting).
740-
See the cookbook for these topics. Also, be sure to lean on the
741-
:doc:`field type reference documentation </reference/forms/types>`, which
742-
includes examples of how to use each field type and its options.
743-
744703
Learn more
745704
----------
746705

0 commit comments

Comments
 (0)