@@ -7,12 +7,8 @@ date Field Type
77A field that allows the user to modify date information via a variety of
88different HTML elements.
99
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.
1612
1713+----------------------+-----------------------------------------------------------------------------+
1814| Underlying Data Type| can be ``DateTime ``, string, timestamp, or array (see the ``input `` option)|
@@ -57,24 +53,68 @@ options are ``input`` and ``widget``.
5753
5854Suppose that you have a ``publishedAt `` field whose underlying date is a
5955``DateTime `` object. The following configures the ``date `` type for that
60- field as three different choice fields::
56+ field as** three different choice fields ** ::
6157
6258 $builder->add('publishedAt', 'date', array(
63- 'input' => 'datetime',
6459 'widget' => 'choice',
6560 ));
6661
67- The ``input `` option *must * be changed to match the type of the underlying
68- date data. For example, if the ``publishedAt `` field's data were a unix
69- timestamp, you'd need to set ``input `` to ``timestamp ``::
62+ If your underlying date is *not * a ``DateTime `` object (e.g. it's a unix timestamp),
63+ configure the `input `_ option.
64+
65+ Rendering a single HTML5 Textbox
66+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67+
68+ For a better user experience, you may want to render a single text field and use
69+ some kind of "date picker" to help your user fill in the right format. To do that,
70+ use the ``single_text `` widget::
7071
7172 $builder->add('publishedAt', 'date', array(
72- 'input' => 'timestamp',
73- 'widget' => 'choice ',
73+ // render as a single text box
74+ 'widget' => 'single_text ',
7475 ));
7576
76- The field also supports an ``array `` and ``string `` as valid ``input `` option
77- values.
77+ This will render as an ``input type="date" `` HTML5 field, which means that **some -
78+ but not all - browsers will add nice date picker functionality to the field **. If you
79+ want to be absolutely sure that *every * user has a consistent date picker, use an
80+ external JavaScript library.
81+
82+ For example, suppose you want to use the `Bootstrap Datepicker `_ library. First,
83+ make the following changes::
84+
85+ $builder->add('publishedAt', 'date', array(
86+ 'widget' => 'single_text',
87+
88+ // do not render as type="date", to avoid HTML5 date pickers
89+ 'html5' => false,
90+
91+ // add a class that can eb selected in JavaScript
92+ 'attr' => ['class' => 'js-datepicker'],
93+ ));
94+
95+ Assuming you're using jQuery, you can initialize the date picker via:
96+
97+ ..code-block ::html
98+
99+ <script >
100+ $ (document ).ready (function () {
101+ $ (' .js-datepicker' ).datepicker ({
102+ format: ' yyyy-mm-dd'
103+ });
104+ });
105+ </script >
106+
107+ This ``format `` key tells the date picker to use the date format that Symfony expects.
108+ This can be tricky: if the date picker is misconfigured, Symfony won't understand
109+ the format and will throw a validation error. You can also configure the format
110+ that Symfony should expect via the `format `_ option.
111+
112+ ..caution ::
113+
114+ The string used by a JavaScript date picker to describe its format (e.g. ``yyyy-mm-dd ``)
115+ may not match the string that Symfony uses (e.g. ``yyyy-MM-dd ``). This is because
116+ different libraries use different formatting rules to describe the date format.
117+ Be aware of this - it can be tricky to make the formats truly match!
78118
79119Field Options
80120-------------
@@ -181,3 +221,5 @@ Field Variables
181221+--------------+------------+----------------------------------------------------------------------+
182222| date_pattern| ``string ``| A string with the date format to use.|
183223+--------------+------------+----------------------------------------------------------------------+
224+
225+ .. _`Bootstrap Datepicker` :https://github.com/eternicode/bootstrap-datepicker