Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
Adding information about using the date type as usable date picker field#6554
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -7,12 +7,8 @@ date Field Type | ||
A field that allows the user to modify date information via a variety of | ||
different HTML elements. | ||
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) | | ||
@@ -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**:: | ||
$builder->add('publishedAt', 'date', array( | ||
'widget' => 'choice', | ||
)); | ||
If your underlying date is *not* a ``DateTime`` object (e.g. it's a unix timestamp), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Unix There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
// render as a single text box | ||
'widget' => 'single_text', | ||
)); | ||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
------------- | ||
@@ -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 |