@@ -145,35 +145,27 @@ helper functions:
145
145
.. code-block :: html+jinja
146
146
147
147
{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}
148
- <form action="{{ path('task_new') }}" method="post" {{ form_enctype(form) }}>
149
- {{ form_widget(form) }}
150
-
151
- <input type="submit" />
152
- </form>
148
+ {{ form(form) }}
153
149
154
150
.. code-block :: html+php
155
151
156
152
<!-- src/Acme/TaskBundle/Resources/views/Default/new.html.php -->
157
- <form action="<?php echo $view['router']->generate('task_new') ?>" method="post" <?php echo $view['form']->enctype($form) ?> >
158
- <?php echo $view['form']->widget($form) ?>
159
-
160
- <input type="submit" />
161
- </form>
153
+ <?php echo $view['form']->form($form) ?>
162
154
163
155
.. image :: /images/book/form-simple.png
164
156
:align: center
165
157
166
158
.. note ::
167
159
168
- This example assumes that you've created a route called `` task_new ``
169
- that points to the `` AcmeTaskBundle:Default:new `` controller that
170
- was created earlier .
160
+ This example assumes that you submit the form in a "POST" request and to
161
+ the same URL that it was displayed in. You will learn later how to
162
+ change the request method and the target URL of the form .
171
163
172
- That's it! By printing ``form_widget (form) ``, each field in the form is
173
- rendered, along with a label and error message (if there is one). As easy
174
- as this is, it's not very flexible (yet). Usually, you'll want to render each
175
- form field individually so you can control how the form looks. You'll learn how
176
- to do that in the ":ref: `form-rendering-template `" section.
164
+ That's it! By printing ``form (form) ``, each field in the form is rendered, along
165
+ with a label and error message (if there is one). As easy as this is, it's not
166
+ very flexible (yet). Usually, you'll want to render each form field individually
167
+ so you can control how the form looks. You'll learn how to do that in the
168
+ ":ref: `form-rendering-template `" section.
177
169
178
170
Before moving on, notice how the rendered ``task `` input field has the value
179
171
of the ``task `` property from the ``$task `` object (i.e. "Write a blog post").
@@ -605,35 +597,30 @@ of code. Of course, you'll usually need much more flexibility when rendering:
605
597
.. code-block :: html+jinja
606
598
607
599
{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}
608
- <form action=" {{ path('task_new') }}" method="post" {{ form_enctype( form) }}>
600
+ {{ form_start( form) }}
609
601
{{ form_errors(form) }}
610
602
611
603
{{ form_row(form.task) }}
612
604
{{ form_row(form.dueDate) }}
613
605
614
- {{ form_rest(form) }}
615
-
616
606
<input type="submit" />
617
- </ form>
607
+ {{ form_end( form) }}
618
608
619
609
.. code-block :: html+php
620
610
621
611
<!-- src/Acme/TaskBundle/Resources/views/Default/newAction.html.php -->
622
- <form action="< ?php echo $view['router']->generate('task_new') ?>" method="post" <?php echo $view[' form']->enctype ($form) ?> >
612
+ <?php echo $view['form']->start ($form) ?>
623
613
<?php echo $view['form']->errors($form) ?>
624
614
625
615
<?php echo $view['form']->row($form['task']) ?>
626
616
<?php echo $view['form']->row($form['dueDate']) ?>
627
617
628
- <?php echo $view['form']->rest($form) ?>
629
-
630
618
<input type="submit" />
631
- </ form>
619
+ <?php echo $view[' form']->end($form) ? >
632
620
633
621
Take a look at each part:
634
622
635
- * ``form_enctype(form) `` - If at least one field is a file upload field, this
636
- renders the obligatory ``enctype="multipart/form-data" ``;
623
+ * ``form_start(form) `` - Renders the start tag of the form.
637
624
638
625
* ``form_errors(form) `` - Renders any errors global to the whole form
639
626
(field-specific errors are displayed next to each field);
@@ -642,10 +629,8 @@ Take a look at each part:
642
629
form widget for the given field (e.g. ``dueDate ``) inside, by default, a
643
630
``div `` element;
644
631
645
- * ``form_rest(form) `` - Renders any fields that have not yet been rendered.
646
- It's usually a good idea to place a call to this helper at the bottom of
647
- each form (in case you forgot to output a field or don't want to bother
648
- manually rendering hidden fields). This helper is also useful for taking
632
+ * ``form_end() `` - Renders the end tag of the form and any fields that have not
633
+ yet been rendered. This is useful for rendering hidden fields and taking
649
634
advantage of the automatic :ref: `CSRF Protection<forms-csrf> `.
650
635
651
636
The majority of the work is done by the ``form_row `` helper, which renders
@@ -740,7 +725,7 @@ field:
740
725
741
726
.. code-block :: html+jinja
742
727
743
- {{ form_widget(form.task, { 'attr': {'class': 'task_field'} }) }}
728
+ {{ form_widget(form.task, {'attr': {'class': 'task_field'}}) }}
744
729
745
730
.. code-block :: html+php
746
731
@@ -783,6 +768,75 @@ available in the :doc:`reference manual</reference/forms/twig_reference>`.
783
768
Read this to know everything about the helpers available and the options
784
769
that can be used with each.
785
770
771
+ .. index ::
772
+ single: Forms; Changing the action and method
773
+
774
+ .. _book-forms-changing-action-and-method :
775
+
776
+ Changing the Action and Method of a Form
777
+ ----------------------------------------
778
+
779
+ So far, we have used the ``form_start() `` helper to render the form's start tag
780
+ and assumed that each form is submitted to the same URL in a POST request.
781
+ Sometimes you want to change these parameters. You can do so in a few different
782
+ ways. If you build your form in the controller, you can use ``setAction() `` and
783
+ ``setMethod() ``::
784
+
785
+ $form = $this->createFormBuilder($task)
786
+ ->setAction($this->generateUrl('target_route'))
787
+ ->setMethod('GET')
788
+ ->add('task', 'text')
789
+ ->add('dueDate', 'date')
790
+ ->getForm();
791
+
792
+ .. note ::
793
+
794
+ This example assumes that you've created a route called ``target_route ``
795
+ that points to the controller that processes the form.
796
+
797
+ In :ref: `book-form-creating-form-classes ` you will learn how to outsource the
798
+ form building code into separate classes. When using such a form class in the
799
+ controller, you can pass the action and method as form options::
800
+
801
+ $form = $this->createForm(new TaskType(), $task, array(
802
+ 'action' => $this->generateUrl('target_route'),
803
+ 'method' => 'GET',
804
+ ));
805
+
806
+ At last, you can override the action and method in the template by passing them
807
+ to the ``form() `` or the ``form_start() `` helper:
808
+
809
+ .. configuration-block ::
810
+
811
+ .. code-block :: html+jinja
812
+
813
+ {# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}
814
+ {{ form(form, {'action': path('target_route'), 'method': 'GET'}) }}
815
+
816
+ {{ form_start(form, {'action': path('target_route'), 'method': 'GET'}) }}
817
+
818
+ .. code-block :: html+php
819
+
820
+ <!-- src/Acme/TaskBundle/Resources/views/Default/newAction.html.php -->
821
+ <?php echo $view['form']->form($form, array(
822
+ 'action' => $view['router']->generate('target_route'),
823
+ 'method' => 'GET',
824
+ )) ?>
825
+
826
+ <?php echo $view['form']->start($form, array(
827
+ 'action' => $view['router']->generate('target_route'),
828
+ 'method' => 'GET',
829
+ )) ?>
830
+
831
+ .. note ::
832
+
833
+ If the form's method is not GET or POST, but PUT, PATCH or DELETE, Symfony2
834
+ will insert a hidden field with the name "_method" that stores this method.
835
+ The form will be submitted in a normal POST request, but Symfony2's router
836
+ is capable of detecting the "_method" parameter and will interpret the
837
+ request as PUT, PATCH or DELETE request. Read the cookbook chapter
838
+ ":doc: `/cookbook/routing/method_parameters `" for more information.
839
+
786
840
.. index ::
787
841
single: Forms; Creating form classes
788
842
@@ -1143,7 +1197,7 @@ renders the form:
1143
1197
1144
1198
{% form_theme form 'AcmeTaskBundle:Form: fields.html.twig' 'AcmeTaskBundle:Form: fields2.html.twig' %}
1145
1199
1146
- < form ...>
1200
+ {{ form(form) }}
1147
1201
1148
1202
.. code-block :: html+php
1149
1203
@@ -1152,7 +1206,7 @@ renders the form:
1152
1206
1153
1207
<?php $view['form']->setTheme($form, array('AcmeTaskBundle:Form', 'AcmeTaskBundle:Form')) ?>
1154
1208
1155
- <form ... >
1209
+ <?php echo $view[' form']->form($form) ? >
1156
1210
1157
1211
The ``form_theme `` tag (in Twig) "imports" the fragments defined in the given
1158
1212
template and uses them when rendering the form. In other words, when the
@@ -1231,7 +1285,7 @@ are 4 possible *parts* of a form that can be rendered:
1231
1285
1232
1286
.. note ::
1233
1287
1234
- There are actually 3 other *parts * - ``rows ``, `` rest ``, and ``enctype `` -
1288
+ There are actually 2 other *parts * - ``rows `` and ``rest `` -
1235
1289
but you should rarely if ever need to worry about overriding them.
1236
1290
1237
1291
By knowing the field type (e.g. ``textarea ``) and which part you want to
0 commit comments