Skip to content

Commit c340e32

Browse files
committed
minor symfony#12145 Updated the main Form article (javiereguiluz)
This PR was squashed before being merged into the 4.3 branch (closes symfony#12145). Discussion ---------- Updated the main Form article Following the recent trend of updating the most popular articles, this PR updates the main forms article a bit. Commits ------- 3f0406f Updated the main Form article
2 parents 999ef88 + 3f0406f commit c340e32

File tree

13 files changed

+554
-554
lines changed

13 files changed

+554
-554
lines changed

_build/redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,4 @@
443443
/routing/hostname_pattern /routing
444444
/routing/extra_information /routing
445445
/console/request_context /routing
446+
/form/action_method /forms

best_practices/forms.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ Handling a form submit usually follows a similar template::
212212
// render the template
213213
}
214214

215+
.. _best-practice-handle-form:
216+
215217
We recommend that you use a single action for both rendering the form and
216218
handling the form submit. For example, you *could* have a ``new()`` action that
217219
*only* renders the form and a ``create()`` action that *only* processes the form

components/form.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ object to read data off of the correct PHP superglobals (i.e. ``$_POST`` or
8080
.. seealso::
8181

8282
If you need more control over exactly when your form is submitted or which
83-
data is passed to it, you can use the :method:`Symfony\\Component\\Form\\FormInterface::submit`
84-
for this. Read more about it :ref:`form-call-submit-directly`.
83+
data is passed to it,
84+
:doc:`use the submit() method to handle form submissions </form/direct_submit>`.
8585

8686
.. sidebar:: Integration with the HttpFoundation Component
8787

doctrine.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ Take a look at the previous example in more detail:
418418
Whether you're creating or updating objects, the workflow is always the same: Doctrine
419419
is smart enough to know if it should INSERT or UPDATE your entity.
420420

421+
.. _automatic_object_validation:
422+
421423
Validating Objects
422424
------------------
423425

form/action_method.rst

Lines changed: 0 additions & 132 deletions
This file was deleted.

form/create_custom_field_type.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ These are the most important methods that a form type class can define:
137137

138138
``buildForm()``
139139
It adds and configures other types into this type. It's the same method used
140-
when :ref:`creating Symfony form classes <form-creating-form-classes>`.
140+
when :ref:`creating Symfony form classes <creating-forms-in-classes>`.
141141

142142
``buildView()``
143143
It sets any extra variables you'll need when rendering the field in a template.

form/direct_submit.rst

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,18 @@
44
How to Use the submit() Function to Handle Form Submissions
55
===========================================================
66

7-
Handle the form submission with the ``handleRequest()`` method::
7+
The recommended way of :ref:`processing Symfony forms <processing-forms>` is to
8+
use the :method:`Symfony\\Component\\Form\\FormInterface::handleRequest` method
9+
to detect when the form has been submitted. However, you can also use the
10+
:method:`Symfony\\Component\\Form\\FormInterface::submit` method to have better
11+
control over when exactly your form is submitted and what data is passed to it::
812

913
use Symfony\Component\HttpFoundation\Request;
1014
// ...
1115

1216
public function new(Request $request)
1317
{
14-
$form = $this->createFormBuilder()
15-
// ...
16-
->getForm();
17-
18-
$form->handleRequest($request);
19-
20-
if ($form->isSubmitted() && $form->isValid()) {
21-
// perform some action...
22-
23-
return $this->redirectToRoute('task_success');
24-
}
25-
26-
return $this->render('product/new.html.twig', [
27-
'form' => $form->createView(),
28-
]);
29-
}
30-
31-
.. tip::
32-
33-
To see more about this method, read :ref:`form-handling-form-submissions`.
34-
35-
.. _form-call-submit-directly:
36-
37-
Calling Form::submit() manually
38-
-------------------------------
39-
40-
In some cases, you want better control over when exactly your form is submitted
41-
and what data is passed to it. Instead of using the
42-
:method:`Symfony\\Component\\Form\\FormInterface::handleRequest`
43-
method, pass the submitted data directly to
44-
:method:`Symfony\\Component\\Form\\FormInterface::submit`::
45-
46-
use Symfony\Component\HttpFoundation\Request;
47-
// ...
48-
49-
public function new(Request $request)
50-
{
51-
$form = $this->createFormBuilder()
52-
// ...
53-
->getForm();
18+
$form = $this->createForm(TaskType::class, $task);
5419

5520
if ($request->isMethod('POST')) {
5621
$form->submit($request->request->get($form->getName()));
@@ -62,7 +27,7 @@ method, pass the submitted data directly to
6227
}
6328
}
6429

65-
return $this->render('product/new.html.twig', [
30+
return $this->render('task/new.html.twig', [
6631
'form' => $form->createView(),
6732
]);
6833
}

form/form_themes.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Symfony Built-In Form Themes
1515

1616
Symfony comes with several **built-in form themes** that make your forms look
1717
great when using some of the most popular CSS frameworks. Each theme is defined
18-
in a single Twig template:
18+
in a single Twig template and they are enabled in the
19+
:ref:`twig.form_themes <config-twig-form-themes>` option:
1920

2021
* `form_div_layout.html.twig`_, wraps each form field inside a ``<div>`` element
2122
and it's the theme used by default in Symfony applications unless you configure
@@ -182,6 +183,8 @@ of form themes:
182183
183184
{# ... #}
184185
186+
.. _create-your-own-form-theme:
187+
185188
Creating your Own Form Theme
186189
----------------------------
187190

form/validation_groups.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ Validation Groups
88
-----------------
99

1010
If your object takes advantage of :doc:`validation groups </validation/groups>`,
11-
you'll need to specify which validation group(s) your form should use::
11+
you'll need to specify which validation group(s) your form should use. Pass
12+
this as an option when :ref:`creating forms in controllers <creating-forms-in-controllers>`::
1213

1314
$form = $this->createFormBuilder($user, [
1415
'validation_groups' => ['registration'],
1516
])->add(...);
1617

17-
If you're creating :ref:`form classes <form-creating-form-classes>` (a good
18-
practice), then you'll need to add the following to the ``configureOptions()``
19-
method::
18+
When :ref:`creating forms in classes <creating-forms-in-classes>`, add the
19+
following to the ``configureOptions()`` method::
2020

2121
use Symfony\Component\OptionsResolver\OptionsResolver;
2222

2323
public function configureOptions(OptionsResolver $resolver)
2424
{
2525
$resolver->setDefaults([
26+
// ...
2627
'validation_groups' => ['registration'],
2728
]);
2829
}

0 commit comments

Comments
 (0)