-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
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) | | ||
|
@@ -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), | ||
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, | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
the official trademark is UNIX, but let's do Unix