Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding information about using the date type as usable date picker field #6554

Merged
merged 1 commit into from
May 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions reference/forms/types/date.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ date Field Type
A field that allows the user to modify date information via a variety of
different HTML elements.

The underlying data used for this field type can be a ``DateTime`` object,
a string, a timestamp or an array. As long as the `input`_ option is set
correctly, the field will take care of all of the details.

The field can be rendered as a single text box, three text boxes (month,
day and year) or three select boxes (see the `widget`_ option).
This field can be rendered in a variety of different ways via the `widget`_ option
and can understand a number of different input formats via the `input`_ option.

+----------------------+-----------------------------------------------------------------------------+
| Underlying Data Type | can be ``DateTime``, string, timestamp, or array (see the ``input`` option) |
Expand Down Expand Up @@ -56,24 +52,68 @@ options are ``input`` and ``widget``.

Suppose that you have a ``publishedAt`` field whose underlying date is a
``DateTime`` object. The following configures the ``date`` type for that
field as three different choice fields::
field as **three different choice fields**::

$builder->add('publishedAt', 'date', array(
'input' => 'datetime',
'widget' => 'choice',
));

The ``input`` option *must* be changed to match the type of the underlying
date data. For example, if the ``publishedAt`` field's data were a unix
timestamp, you'd need to set ``input`` to ``timestamp``::
If your underlying date is *not* a ``DateTime`` object (e.g. it's a unix timestamp),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the official trademark is UNIX, but let's do Unix

configure the `input`_ option.

Rendering a single HTML5 Textbox
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For a better user experience, you may want to render a single text field and use
some kind of "date picker" to help your user fill in the right format. To do that,
use the ``single_text`` widget::

$builder->add('publishedAt', 'date', array(
'input' => 'timestamp',
'widget' => 'choice',
// render as a single text box
'widget' => 'single_text',
));

The field also supports an ``array`` and ``string`` as valid ``input`` option
values.
This will render as an ``input type="date"`` HTML5 field, which means that **some -
but not all - browsers will add nice date picker functionality to the field**. If you
want to be absolutely sure that *every* user has a consistent date picker, use an
external JavaScript library.

For example, suppose you want to use the `Bootstrap Datepicker`_ library. First,
make the following changes::

$builder->add('publishedAt', 'date', array(
'widget' => 'single_text',

// do not render as type="date", to avoid HTML5 date pickers
'html5' => false,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, this is needed, otherwise your browser's native HTML5 date picker will compete/fight with the JavaScript one


// add a class that can eb selected in JavaScript
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be

'attr' => ['class' => 'js-datepicker'],
));

Assuming you're using jQuery, you can initialize the date picker via:

.. code-block:: html

<script>
$(document).ready(function() {
$('.js-datepicker').datepicker({
format: 'yyyy-mm-dd'
});
});
</script>

This ``format`` key tells the date picker to use the date format that Symfony expects.
This can be tricky: if the date picker is misconfigured, Symfony won't understand
the format and will throw a validation error. You can also configure the format
that Symfony should expect via the `format`_ option.

.. caution::

The string used by a JavaScript date picker to describe its format (e.g. ``yyyy-mm-dd``)
may not match the string that Symfony uses (e.g. ``yyyy-MM-dd``). This is because
different libraries use different formatting rules to describe the date format.
Be aware of this - it can be tricky to make the formats truly match!

Field Options
-------------
Expand Down Expand Up @@ -171,3 +211,5 @@ Field Variables
+--------------+------------+----------------------------------------------------------------------+
| date_pattern | ``string`` | A string with the date format to use. |
+--------------+------------+----------------------------------------------------------------------+

.. _`Bootstrap Datepicker`: https://github.com/eternicode/bootstrap-datepicker