@@ -356,7 +356,7 @@ corresponding errors printed out with the form.
356
356
a ``required `` attribute on fields that are required. For browsers that
357
357
support HTML5, this will result in a native browser message being displayed
358
358
if the user tries to submit the form with that field blank.
359
-
359
+
360
360
Generated forms take full advantage of this new feature by adding sensible
361
361
HTML attributes that trigger the validation. The client-side validation,
362
362
however, can be disabled by adding the ``novalidate `` attribute to the
@@ -484,13 +484,13 @@ the documentation for each type.
484
484
that HTML5-ready browsers will apply client-side validation if the field
485
485
is left blank. If you don't want this behavior, either set the ``required ``
486
486
option on your field to ``false `` or :ref: `disable HTML5 validation<book-forms-html5-validation-disable> `.
487
-
487
+
488
488
Also note that setting the ``required `` option to ``true `` will **not **
489
489
result in server-side validation to be applied. In other words, if a
490
490
user submits a blank value for the field (either with an old browser
491
491
or web service, for example), it will be accepted as a valid value unless
492
492
you use Symfony's ``NotBlank `` or ``NotNull `` validation constraint.
493
-
493
+
494
494
In other words, the ``required `` option is "nice", but true server-side
495
495
validation should *always * be used.
496
496
@@ -569,7 +569,7 @@ the correct values of a number of field options.
569
569
option can be guessed from the validation constrains (if ``MinLength ``
570
570
or ``Min `` is used) or from the Doctrine metadata (via the field's length).
571
571
572
- * ``max_length ``: Similar to ``min_length ``, the maximum length can also
572
+ * ``max_length ``: Similar to ``min_length ``, the maximum length can also
573
573
be guessed.
574
574
575
575
.. note ::
@@ -651,11 +651,11 @@ output can be customized on many different levels.
651
651
.. tip ::
652
652
653
653
You can access the current data of your form via ``form.vars.value ``:
654
-
654
+
655
655
.. configuration-block ::
656
656
657
657
.. code-block :: jinja
658
-
658
+
659
659
{{ form.vars.value.task }}
660
660
661
661
.. code-block :: html+php
@@ -725,7 +725,7 @@ specify it:
725
725
726
726
<?php echo $view['form']->label($form['task'], 'Task Description') ?>
727
727
728
- Finally, some field types have additional rendering options that can be passed
728
+ Some field types have additional rendering options that can be passed
729
729
to the widget. These options are documented with each type, but one common
730
730
options is ``attr ``, which allows you to modify attributes on the form element.
731
731
The following would add the ``task_field `` class to the rendered input text
@@ -743,6 +743,33 @@ field:
743
743
'attr' => array('class' => 'task_field'),
744
744
)) ?>
745
745
746
+ If you need to render form fields "by hand" then you can access individual
747
+ values for fields such as the ``id ``, ``name `` and ``label ``. For example
748
+ to get the ``id ``:
749
+
750
+ .. configuration-block ::
751
+
752
+ .. code-block :: html+jinja
753
+
754
+ {{ form.task.vars.id }}
755
+
756
+ .. code-block :: html+php
757
+
758
+ <?php echo $form['task']->get('id') ?>
759
+
760
+ To get the value used for the form field's name attribute you need to use
761
+ the ``full_name `` value:
762
+
763
+ .. configuration-block ::
764
+
765
+ .. code-block :: html+jinja
766
+
767
+ {{ form.task.vars.full_name }}
768
+
769
+ .. code-block :: html+php
770
+
771
+ <?php echo $form['task']->get('full_name') ?>
772
+
746
773
Twig Template Function Reference
747
774
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
748
775
@@ -829,6 +856,25 @@ the choice is ultimately up to you.
829
856
);
830
857
}
831
858
859
+ .. tip ::
860
+
861
+ When mapping forms to objects, all fields are mapped. Any fields on the
862
+ form that do not exist on the mapped object will cause an exception to
863
+ be thrown.
864
+
865
+ In cases where you need extra fields in the form (for example: a "do you
866
+ agree with these terms" checkbox) that will not be mapped to the underlying
867
+ object, you need to set the property_path option to ``false ``::
868
+
869
+ public function buildForm(FormBuilder $builder, array $options)
870
+ {
871
+ $builder->add('task');
872
+ $builder->add('dueDate', null, array('property_path' => false));
873
+ }
874
+
875
+ Additionally, if there are any fields on the form that aren't included in
876
+ the submitted data, those fields will be explicitly set to ``null ``.
877
+
832
878
.. index ::
833
879
pair: Forms; Doctrine
834
880
@@ -1076,6 +1122,8 @@ renders the form:
1076
1122
1077
1123
{% form_theme form 'AcmeTaskBundle:Form: fields.html.twig' %}
1078
1124
1125
+ {% form_theme form 'AcmeTaskBundle:Form: fields.html.twig' 'AcmeTaskBundle:Form: fields2.html.twig' %}
1126
+
1079
1127
<form ...>
1080
1128
1081
1129
.. code-block :: html+php
@@ -1084,6 +1132,8 @@ renders the form:
1084
1132
1085
1133
<?php $view['form']->setTheme($form, array('AcmeTaskBundle:Form')) ?>
1086
1134
1135
+ <?php $view['form']->setTheme($form, array('AcmeTaskBundle:Form', 'AcmeTaskBundle:Form')) ?>
1136
+
1087
1137
<form ...>
1088
1138
1089
1139
The ``form_theme `` tag (in Twig) "imports" the fragments defined in the given
@@ -1092,6 +1142,13 @@ template and uses them when rendering the form. In other words, when the
1092
1142
block from your custom theme (instead of the default ``field_row `` block
1093
1143
that ships with Symfony).
1094
1144
1145
+ Your custom theme does not have to override all the blocks. When rendering a block
1146
+ which is not overridden in your custom theme, the theming engine will fall back
1147
+ to the global theme (defined at the bundle level).
1148
+
1149
+ If several custom themes are provided they will be searched in the listed order
1150
+ before falling back to the global theme.
1151
+
1095
1152
To customize any portion of a form, you just need to override the appropriate
1096
1153
fragment. Knowing exactly which block or file to override is the subject of
1097
1154
the next section.
@@ -1339,7 +1396,7 @@ The CSRF token can be customized on a form-by-form basis. For example::
1339
1396
class TaskType extends AbstractType
1340
1397
{
1341
1398
// ...
1342
-
1399
+
1343
1400
public function getDefaultOptions(array $options)
1344
1401
{
1345
1402
return array(
@@ -1350,7 +1407,7 @@ The CSRF token can be customized on a form-by-form basis. For example::
1350
1407
'intention' => 'task_item',
1351
1408
);
1352
1409
}
1353
-
1410
+
1354
1411
// ...
1355
1412
}
1356
1413
@@ -1389,14 +1446,14 @@ an array of the submitted data. This is actually really easy::
1389
1446
->add('email', 'email')
1390
1447
->add('message', 'textarea')
1391
1448
->getForm();
1392
-
1449
+
1393
1450
if ($request->getMethod() == 'POST') {
1394
1451
$form->bindRequest($request);
1395
1452
1396
1453
// data is an array with "name", "email", and "message" keys
1397
1454
$data = $form->getData();
1398
1455
}
1399
-
1456
+
1400
1457
// ... render the form
1401
1458
}
1402
1459
@@ -1416,14 +1473,14 @@ an array.
1416
1473
1417
1474
.. tip ::
1418
1475
1419
- You can also access POST values (in this case "name") directly through
1476
+ You can also access POST values (in this case "name") directly through
1420
1477
the request object, like so:
1421
1478
1422
1479
.. code-block :: php
1423
1480
1424
1481
$this->get('request')->request->get('name');
1425
1482
1426
- Be advised, however, that in most cases using the getData() method is
1483
+ Be advised, however, that in most cases using the getData() method is
1427
1484
a better choice, since it returns the data (usually an object) after
1428
1485
it's been transformed by the form framework.
1429
1486
@@ -1456,7 +1513,7 @@ but here's a short example::
1456
1513
// ...
1457
1514
;
1458
1515
1459
- Now, when you call `$form->isValid( ) `, the constraints setup here are run
1516
+ Now, when you call `$form->bindRequest($request ) `, the constraints setup here are run
1460
1517
against your form's data. If you're using a form class, override the ``getDefaultOptions ``
1461
1518
method to specify the option::
1462
1519
@@ -1478,15 +1535,15 @@ method to specify the option::
1478
1535
'name' => new MinLength(5),
1479
1536
'email' => new Email(array('message' => 'Invalid email address')),
1480
1537
));
1481
-
1538
+
1482
1539
return array('validation_constraint' => $collectionConstraint);
1483
1540
}
1484
1541
}
1485
1542
1486
1543
Now, you have the flexibility to create forms - with validation - that return
1487
1544
an array of data, instead of an object. In most cases, it's better - and
1488
1545
certainly more robust - to bind your form to an object. But for simple forms,
1489
- this is a great approach.
1546
+ this is a great approach.
1490
1547
1491
1548
Final Thoughts
1492
1549
--------------
0 commit comments