Skip to content

Commit d963be9

Browse files
committed
Merge branch 'issue5383' of github.com:bschussek/symfony-docs into bschussek-issue5383
Conflicts: book/forms.rst
2 parents 40af9fa + 88b88e5 commit d963be9

File tree

11 files changed

+293
-7
lines changed

11 files changed

+293
-7
lines changed

book/forms.rst

Lines changed: 122 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ from inside a controller::
101101
$form = $this->createFormBuilder($task)
102102
->add('task', 'text')
103103
->add('dueDate', 'date')
104+
->add('save', 'submit')
104105
->getForm();
105106

106107
return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
@@ -125,6 +126,11 @@ In this example, you've added two fields to your form - ``task`` and ``dueDate``
125126
corresponding to the ``task`` and ``dueDate`` properties of the ``Task`` class.
126127
You've also assigned each a "type" (e.g. ``text``, ``date``), which, among
127128
other things, determines which HTML form tag(s) is rendered for that field.
129+
At last, you added a submit button for submitting the form to the server.
130+
131+
.. versionadded:: 2.3
132+
Support for submit buttons was added in Symfony 2.3. Before that, you had
133+
to add buttons to the form's HTML manually.
128134

129135
Symfony2 comes with many built-in types that will be discussed shortly
130136
(see :ref:`book-forms-type-reference`).
@@ -145,11 +151,13 @@ helper functions:
145151
.. code-block:: html+jinja
146152

147153
{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}
154+
148155
{{ form(form) }}
149156

150157
.. code-block:: html+php
151158

152159
<!-- src/Acme/TaskBundle/Resources/views/Default/new.html.php -->
160+
153161
<?php echo $view['form']->form($form) ?>
154162

155163
.. image:: /images/book/form-simple.png
@@ -187,7 +195,7 @@ it into a format that's suitable for being rendered in an HTML form.
187195
Support for "hasser" methods was added in Symfony 2.1.
188196

189197
.. index::
190-
single: Forms; Handling form submission
198+
single: Forms; Handling form submissions
191199

192200
.. _book-form-handling-form-submissions:
193201

@@ -210,6 +218,7 @@ controller::
210218
$form = $this->createFormBuilder($task)
211219
->add('task', 'text')
212220
->add('dueDate', 'date')
221+
->add('save', 'submit')
213222
->getForm();
214223

215224
$form->handleRequest($request);
@@ -262,6 +271,42 @@ possible paths:
262271
Redirecting a user after a successful form submission prevents the user
263272
from being able to hit "refresh" and re-post the data.
264273

274+
.. index::
275+
single: Forms; Multiple Submit Buttons
276+
277+
.. _book-form-submitting-multiple-buttons:
278+
279+
Submitting Forms with Multiple Buttons
280+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
281+
282+
.. versionadded:: 2.3
283+
Support for buttons in forms was added in Symfony 2.3.
284+
285+
When your form contains more than one submit button, you will want to check
286+
which of the buttons was clicked to adapt the program flow in your controller.
287+
Let's add a second button with the caption "Save and add" to our form::
288+
289+
$form = $this->createFormBuilder($task)
290+
->add('task', 'text')
291+
->add('dueDate', 'date')
292+
->add('save', 'submit')
293+
->add('saveAndAdd', 'submit')
294+
->getForm();
295+
296+
In your controller, use the button's
297+
:method:`Symfony\\Component\\Form\\ClickableInterface::isClicked` method for
298+
querying if the "Save and add" button was clicked::
299+
300+
if ($form->isValid()) {
301+
// ... perform some action, such as saving the task to the database
302+
303+
$nextAction = $form->get('saveAndAdd')->isClicked()
304+
? 'task_new'
305+
: 'task_success';
306+
307+
return $this->redirect($this->generateUrl($nextAction));
308+
}
309+
265310
.. index::
266311
single: Forms; Validation
267312

@@ -405,16 +450,50 @@ method::
405450
In both of these cases, *only* the ``registration`` validation group will
406451
be used to validate the underlying object.
407452

408-
Groups based on Submitted Data
409-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
453+
.. index::
454+
single: Forms; Disabling validation
455+
456+
Disabling Validation
457+
~~~~~~~~~~~~~~~~~~~~
458+
459+
.. versionadded:: 2.3
460+
The ability to set ``validation_groups`` to false was added in Symfony 2.3,
461+
although setting it to an empty array achieved the same result in previous
462+
versions.
463+
464+
Sometimes it is useful to suppress the validation of a form altogether. For
465+
these cases, you can skip the call to :method:`Symfony\\Component\\Form\\FormInterface::isValid`
466+
in your controller. If this is not possible, you can alternatively set the
467+
``validation_groups`` option to ``false`` or an empty array::
468+
469+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
470+
471+
public function setDefaultOptions(OptionsResolverInterface $resolver)
472+
{
473+
$resolver->setDefaults(array(
474+
'validation_groups' => false,
475+
));
476+
}
477+
478+
Note that when you do that, the form will still run basic integrity checks,
479+
for example whether an uploaded file was too large or whether non-existing
480+
fields were submitted. If you want to suppress validation completely, remove
481+
the :method:`Symfony\\Component\\Form\\FormInterface::isValid` call from your
482+
controller.
483+
484+
.. index::
485+
single: Forms; Validation groups based on submitted data
486+
487+
Groups based on the Submitted Data
488+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
410489

411490
.. versionadded:: 2.1
412491
The ability to specify a callback or Closure in ``validation_groups``
413492
is new to version 2.1
414493

415494
If you need some advanced logic to determine the validation groups (e.g.
416495
based on submitted data), you can set the ``validation_groups`` option
417-
to an array callback, or a ``Closure``::
496+
to an array callback::
418497

419498
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
420499

@@ -428,7 +507,7 @@ to an array callback, or a ``Closure``::
428507
This will call the static method ``determineValidationGroups()`` on the
429508
``Client`` class after the form is submitted, but before validation is executed.
430509
The Form object is passed as an argument to that method (see next example).
431-
You can also define whole logic inline by using a Closure::
510+
You can also define whole logic inline by using a ``Closure``::
432511

433512
use Symfony\Component\Form\FormInterface;
434513
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
@@ -447,6 +526,44 @@ You can also define whole logic inline by using a Closure::
447526
));
448527
}
449528

529+
.. index::
530+
single: Forms; Validation groups based on clicked button
531+
532+
Groups based on the Clicked Button
533+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
534+
535+
.. versionadded:: 2.3
536+
Support for buttons in forms was added in Symfony 2.3.
537+
538+
When your form contains multiple submit buttons, you can change the validation
539+
group depending on which button is used to submit the form. For example,
540+
consider a form in a wizard that lets you advance to the next step or go back
541+
to the previous step. Let's assume also that when returning to the previous
542+
step, the data of the form should be saved, but not validated.
543+
544+
First, we need to add the two buttons to the form::
545+
546+
$form = $this->createFormBuilder($task)
547+
// ...
548+
->add('nextStep', 'submit')
549+
->add('previousStep', 'submit')
550+
->getForm();
551+
552+
Then, we configure the button for returning to the previous step to run
553+
specific validation groups. In this example, we want it to suppress validation,
554+
so we set its ``validation_groups`` options to false::
555+
556+
$form = $this->createFormBuilder($task)
557+
// ...
558+
->add('previousStep', 'submit', array(
559+
'validation_groups' => false,
560+
))
561+
->getForm();
562+
563+
Now the form will skip your validation constraints. It will still validate
564+
basic integrity constraints, such as checking whether an uploaded file was too
565+
large or whether you tried to submit text in a number field.
566+
450567
.. index::
451568
single: Forms; Built-in field types
452569

reference/forms/types.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Form Types Reference
99
:hidden:
1010

1111
types/birthday
12+
types/button
1213
types/checkbox
1314
types/choice
1415
types/collection
@@ -30,7 +31,9 @@ Form Types Reference
3031
types/percent
3132
types/radio
3233
types/repeated
34+
types/reset
3335
types/search
36+
types/submit
3437
types/text
3538
types/textarea
3639
types/time

reference/forms/types/button.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.. index::
2+
single: Forms; Fields; button
3+
4+
button Field Type
5+
=================
6+
7+
.. versionadded:: 2.3
8+
The ``button`` type was added in Symfony 2.3
9+
10+
A simple, non-responsive button.
11+
12+
+----------------------+----------------------------------------------------------------------+
13+
| Rendered as | ``button`` tag |
14+
+----------------------+----------------------------------------------------------------------+
15+
| Options | - `attr`_ |
16+
| | - `disabled`_ |
17+
| | - `label`_ |
18+
| | - `translation_domain`_ |
19+
+----------------------+----------------------------------------------------------------------+
20+
| Parent type | none |
21+
+----------------------+----------------------------------------------------------------------+
22+
| Class | :class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\ButtonType` |
23+
+----------------------+----------------------------------------------------------------------+
24+
25+
Options
26+
-------
27+
28+
.. include:: /reference/forms/types/options/button_attr.rst.inc
29+
30+
.. include:: /reference/forms/types/options/button_disabled.rst.inc
31+
32+
.. include:: /reference/forms/types/options/button_label.rst.inc
33+
34+
.. include:: /reference/forms/types/options/button_translation_domain.rst.inc

reference/forms/types/map.rst.inc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,15 @@ Hidden Fields
4848

4949
* :doc:`hidden</reference/forms/types/hidden>`
5050

51+
Buttons
52+
~~~~~~~
53+
54+
* :doc:`button</reference/forms/types/button>`
55+
* :doc:`reset</reference/forms/types/reset>`
56+
* :doc:`submit</reference/forms/types/submit>`
57+
5158
Base Fields
5259
~~~~~~~~~~~
5360

5461
* :doc:`field</reference/forms/types/field>`
55-
* :doc:`form</reference/forms/types/form>`
62+
* :doc:`form</reference/forms/types/form>`

reference/forms/types/options/attr.rst.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ attr
33

44
**type**: array **default**: Empty array
55

6-
If you want to add extra attributes to HTML field representation
6+
If you want to add extra attributes to the HTML field representation,
77
you can use ``attr`` option. It's an associative array with HTML attribute
88
as a key. This can be useful when you need to set a custom class for some widget::
99

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
attr
2+
~~~~
3+
4+
**type**: array **default**: Empty array
5+
6+
If you want to add extra attributes to the HTML representation of the button,
7+
you can use ``attr`` option. It's an associative array with HTML attribute
8+
as a key. This can be useful when you need to set a custom class for the button::
9+
10+
$builder->add('save', 'button', array(
11+
'attr' => array('class' => 'save'),
12+
));
13+
14+
15+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
disabled
2+
~~~~~~~~
3+
4+
**type**: ``boolean`` **default**: ``false``
5+
6+
If you don't want a user to be able to click a button, you can set the disabled
7+
option to true. It will not be possible to submit the form with this button,
8+
not even when bypassing the browser and sending a request manually, for
9+
example with cURL.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
label
2+
~~~~~
3+
4+
**type**: ``string`` **default**: The label is "guessed" from the field name
5+
6+
Sets the label that will be displayed on the button. The label can also be
7+
directly set inside the template:
8+
9+
.. configuration-block::
10+
11+
.. code-block:: html+jinja
12+
13+
{{ form_widget(form.save, { 'label': 'Click me' }) }}
14+
15+
.. code-block:: html+php
16+
17+
<?php echo $view['form']->widget($form['save'], array('label' => 'Click me')) ?>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
translation_domain
2+
~~~~~~~~~~~~~~~~~~
3+
4+
**type**: ``string`` **default**: ``messages``
5+
6+
This is the translation domain that will be used for any labels or options
7+
that are rendered for this button.

reference/forms/types/reset.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.. index::
2+
single: Forms; Fields; reset
3+
4+
reset Field Type
5+
================
6+
7+
.. versionadded:: 2.3
8+
The ``reset`` type was added in Symfony 2.3
9+
10+
A button that resets all fields to their original values.
11+
12+
+----------------------+---------------------------------------------------------------------+
13+
| Rendered as | ``input`` ``reset`` tag |
14+
+----------------------+---------------------------------------------------------------------+
15+
| Inherited | - `attr`_ |
16+
| options | - `disabled`_ |
17+
| | - `label`_ |
18+
| | - `translation_domain`_ |
19+
+----------------------+---------------------------------------------------------------------+
20+
| Parent type | :doc:`button</reference/forms/types/button>` |
21+
+----------------------+---------------------------------------------------------------------+
22+
| Class | :class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\ResetType` |
23+
+----------------------+---------------------------------------------------------------------+
24+
25+
Inherited options
26+
-----------------
27+
28+
.. include:: /reference/forms/types/options/button_attr.rst.inc
29+
30+
.. include:: /reference/forms/types/options/button_disabled.rst.inc
31+
32+
.. include:: /reference/forms/types/options/button_label.rst.inc
33+
34+
.. include:: /reference/forms/types/options/button_translation_domain.rst.inc

0 commit comments

Comments
 (0)