@@ -101,6 +101,7 @@ from inside a controller::
101
101
$form = $this->createFormBuilder($task)
102
102
->add('task', 'text')
103
103
->add('dueDate', 'date')
104
+ ->add('save', 'submit')
104
105
->getForm();
105
106
106
107
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``
125
126
corresponding to the ``task `` and ``dueDate `` properties of the ``Task `` class.
126
127
You've also assigned each a "type" (e.g. ``text ``, ``date ``), which, among
127
128
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.
128
134
129
135
Symfony2 comes with many built-in types that will be discussed shortly
130
136
(see :ref: `book-forms-type-reference `).
@@ -147,17 +153,13 @@ helper functions:
147
153
{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}
148
154
<form action="{{ path('task_new') }}" method="post" {{ form_enctype(form) }}>
149
155
{{ form_widget(form) }}
150
-
151
- <input type="submit" />
152
156
</form>
153
157
154
158
.. code-block :: html+php
155
159
156
160
<!-- src/Acme/TaskBundle/Resources/views/Default/new.html.php -->
157
161
<form action="<?php echo $view['router']->generate('task_new') ?>" method="post" <?php echo $view['form']->enctype($form) ?> >
158
162
<?php echo $view['form']->widget($form) ?>
159
-
160
- <input type="submit" />
161
163
</form>
162
164
163
165
.. image :: /images/book/form-simple.png
@@ -194,7 +196,7 @@ it into a format that's suitable for being rendered in an HTML form.
194
196
Support for "hasser" methods was added in Symfony 2.1.
195
197
196
198
.. index ::
197
- single: Forms; Handling form submission
199
+ single: Forms; Handling form submissions
198
200
199
201
Handling Form Submissions
200
202
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -215,6 +217,7 @@ controller::
215
217
$form = $this->createFormBuilder($task)
216
218
->add('task', 'text')
217
219
->add('dueDate', 'date')
220
+ ->add('save', 'submit')
218
221
->getForm();
219
222
220
223
if ($request->isMethod('POST')) {
@@ -265,6 +268,42 @@ possible paths:
265
268
Redirecting a user after a successful form submission prevents the user
266
269
from being able to hit "refresh" and re-post the data.
267
270
271
+ .. index ::
272
+ single: Forms; Multiple Submit Buttons
273
+
274
+ .. _book-form-submitting-multiple-buttons :
275
+
276
+ Submitting Forms with Multiple Buttons
277
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278
+
279
+ .. versionadded :: 2.3
280
+ Support for buttons in forms was added in Symfony 2.3.
281
+
282
+ When your form contains more than one submit button, you will want to check
283
+ which of the buttons was clicked to adapt the program flow in your controller.
284
+ Let's add a second button with the caption "Save and add" to our form::
285
+
286
+ $form = $this->createFormBuilder($task)
287
+ ->add('task', 'text')
288
+ ->add('dueDate', 'date')
289
+ ->add('save', 'submit')
290
+ ->add('saveAndAdd', 'submit')
291
+ ->getForm();
292
+
293
+ In your controller, use the button's
294
+ :method: `Symfony\\ Component\\ Form\\ ClickableInterface\\ isClicked ` method for
295
+ querying if the "Save and add" button was clicked::
296
+
297
+ if ($form->isValid()) {
298
+ // perform some action, such as saving the task to the database
299
+
300
+ $nextAction = $form->get('saveAndAdd')->isClicked()
301
+ ? 'task_new'
302
+ : 'task_success';
303
+
304
+ return $this->redirect($this->generateUrl($nextAction));
305
+ }
306
+
268
307
.. index ::
269
308
single: Forms; Validation
270
309
@@ -408,16 +447,49 @@ method::
408
447
In both of these cases, *only * the ``registration `` validation group will
409
448
be used to validate the underlying object.
410
449
411
- Groups based on Submitted Data
412
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
450
+ .. index ::
451
+ single: Forms; Disabling validation
452
+
453
+ Disabling Validation
454
+ ~~~~~~~~~~~~~~~~~~~~
455
+
456
+ .. versionadded :: 2.3
457
+ The ability to set ``validation_groups `` to false is new to version 2.3.
458
+ Setting it to an empty array was already supported before.
459
+
460
+ Sometimes it is useful to suppress the validation of a form altogether. For
461
+ these cases, you can skip the call to :method: `Symfony\\ Component\\ Form\\ FormInterface::isValid `
462
+ in your controller. If this is not possible, you can alternatively set the
463
+ ``validation_groups `` option to ``false `` or an empty array::
464
+
465
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
466
+
467
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
468
+ {
469
+ $resolver->setDefaults(array(
470
+ 'validation_groups' => false,
471
+ ));
472
+ }
473
+
474
+ Note that when you do that, the form will still run basic integrity checks,
475
+ for example whether an uploaded file was too large or whether non-existing
476
+ fields were submitted. If you want to suppress validation completely, remove
477
+ the :method: `Symfony\\ Component\\ Form\\ FormInterface::isValid ` call from your
478
+ controller.
479
+
480
+ .. index ::
481
+ single: Forms; Validation groups based on submitted data
482
+
483
+ Groups based on the Submitted Data
484
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
413
485
414
486
.. versionadded :: 2.1
415
487
The ability to specify a callback or Closure in ``validation_groups ``
416
488
is new to version 2.1
417
489
418
490
If you need some advanced logic to determine the validation groups (e.g.
419
491
based on submitted data), you can set the ``validation_groups `` option
420
- to an array callback, or a `` Closure `` ::
492
+ to an array callback::
421
493
422
494
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
423
495
@@ -431,7 +503,7 @@ to an array callback, or a ``Closure``::
431
503
This will call the static method ``determineValidationGroups() `` on the
432
504
``Client `` class after the form is bound, but before validation is executed.
433
505
The Form object is passed as an argument to that method (see next example).
434
- You can also define whole logic inline by using a Closure::
506
+ You can also define whole logic inline by using a `` Closure `` ::
435
507
436
508
use Symfony\Component\Form\FormInterface;
437
509
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
@@ -450,6 +522,44 @@ You can also define whole logic inline by using a Closure::
450
522
));
451
523
}
452
524
525
+ .. index ::
526
+ single: Forms; Validation groups based on clicked button
527
+
528
+ Groups based on the Clicked Button
529
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
530
+
531
+ .. versionadded :: 2.3
532
+ Support for buttons in forms was added in Symfony 2.3.
533
+
534
+ When your form contains multiple submit buttons, you can change the validation
535
+ group depending on which button is used to submit the form. For example,
536
+ consider a form in a wizard that lets you advance to the next step or go back
537
+ to the previous step. Let's assume also that when returning to the previous
538
+ step, the data of the form should be saved, but not validated.
539
+
540
+ First, we need to add the two buttons to the form::
541
+
542
+ $form = $this->createFormBuilder($task)
543
+ // ...
544
+ ->add('nextStep', 'submit')
545
+ ->add('previousStep', 'submit')
546
+ ->getForm();
547
+
548
+ Then, we configure the button for returning to the previous step to run
549
+ specific validation groups. In this example, we want it to suppress validation,
550
+ so we set its ``validation_groups `` options to false::
551
+
552
+ $form = $this->createFormBuilder($task)
553
+ // ...
554
+ ->add('previousStep', 'submit', array(
555
+ 'validation_groups' => false,
556
+ ))
557
+ ->getForm();
558
+
559
+ Now the form will skip your validation constraints. It will still validate
560
+ basic integrity constraints, such as checking whether an uploaded file was too
561
+ large or whether you tried to submit text in a number field.
562
+
453
563
.. index ::
454
564
single: Forms; Built-in field types
455
565
0 commit comments