@@ -7,12 +7,8 @@ date Field Type
7
7
A field that allows the user to modify date information via a variety of
8
8
different HTML elements.
9
9
10
- The underlying data used for this field type can be a ``DateTime `` object,
11
- a string, a timestamp or an array. As long as the `input `_ option is set
12
- correctly, the field will take care of all of the details.
13
-
14
- The field can be rendered as a single text box, three text boxes (month,
15
- day and year) or three select boxes (see the `widget `_ option).
10
+ This field can be rendered in a variety of different ways via the `widget `_ option
11
+ and can understand a number of different input formats via the `input `_ option.
16
12
17
13
+----------------------+-----------------------------------------------------------------------------+
18
14
| Underlying Data Type | can be ``DateTime ``, string, timestamp, or array (see the ``input `` option) |
@@ -56,24 +52,68 @@ options are ``input`` and ``widget``.
56
52
57
53
Suppose that you have a ``publishedAt `` field whose underlying date is a
58
54
``DateTime `` object. The following configures the ``date `` type for that
59
- field as three different choice fields::
55
+ field as ** three different choice fields ** ::
60
56
61
57
$builder->add('publishedAt', 'date', array(
62
- 'input' => 'datetime',
63
58
'widget' => 'choice',
64
59
));
65
60
66
- The ``input `` option *must * be changed to match the type of the underlying
67
- date data. For example, if the ``publishedAt `` field's data were a unix
68
- timestamp, you'd need to set ``input `` to ``timestamp ``::
61
+ If your underlying date is *not * a ``DateTime `` object (e.g. it's a unix timestamp),
62
+ configure the `input `_ option.
63
+
64
+ Rendering a single HTML5 Textbox
65
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66
+
67
+ For a better user experience, you may want to render a single text field and use
68
+ some kind of "date picker" to help your user fill in the right format. To do that,
69
+ use the ``single_text `` widget::
69
70
70
71
$builder->add('publishedAt', 'date', array(
71
- 'input' => 'timestamp',
72
- 'widget' => 'choice ',
72
+ // render as a single text box
73
+ 'widget' => 'single_text ',
73
74
));
74
75
75
- The field also supports an ``array `` and ``string `` as valid ``input `` option
76
- values.
76
+ This will render as an ``input type="date" `` HTML5 field, which means that **some -
77
+ but not all - browsers will add nice date picker functionality to the field **. If you
78
+ want to be absolutely sure that *every * user has a consistent date picker, use an
79
+ external JavaScript library.
80
+
81
+ For example, suppose you want to use the `Bootstrap Datepicker `_ library. First,
82
+ make the following changes::
83
+
84
+ $builder->add('publishedAt', 'date', array(
85
+ 'widget' => 'single_text',
86
+
87
+ // do not render as type="date", to avoid HTML5 date pickers
88
+ 'html5' => false,
89
+
90
+ // add a class that can eb selected in JavaScript
91
+ 'attr' => ['class' => 'js-datepicker'],
92
+ ));
93
+
94
+ Assuming you're using jQuery, you can initialize the date picker via:
95
+
96
+ .. code-block :: html
97
+
98
+ <script >
99
+ $ (document ).ready (function () {
100
+ $ (' .js-datepicker' ).datepicker ({
101
+ format: ' yyyy-mm-dd'
102
+ });
103
+ });
104
+ </script >
105
+
106
+ This ``format `` key tells the date picker to use the date format that Symfony expects.
107
+ This can be tricky: if the date picker is misconfigured, Symfony won't understand
108
+ the format and will throw a validation error. You can also configure the format
109
+ that Symfony should expect via the `format `_ option.
110
+
111
+ .. caution ::
112
+
113
+ The string used by a JavaScript date picker to describe its format (e.g. ``yyyy-mm-dd ``)
114
+ may not match the string that Symfony uses (e.g. ``yyyy-MM-dd ``). This is because
115
+ different libraries use different formatting rules to describe the date format.
116
+ Be aware of this - it can be tricky to make the formats truly match!
77
117
78
118
Field Options
79
119
-------------
@@ -171,3 +211,5 @@ Field Variables
171
211
+--------------+------------+----------------------------------------------------------------------+
172
212
| date_pattern | ``string `` | A string with the date format to use. |
173
213
+--------------+------------+----------------------------------------------------------------------+
214
+
215
+ .. _`Bootstrap Datepicker` : https://github.com/eternicode/bootstrap-datepicker
0 commit comments