Problem/Motivation

The Date render element exposes a '#date_date_format', which is supposed to be used to specify the date format used for display when the browser doesn't support the HTML5 date widget. Setting it to anything other than 'Y-m-d' will result in an unexpected form value though, because the date polyfill use that format both for display and form submission, instead of being restricted to display. HTML 5 browsers will format the date correctly as Y-m-d, thus causing a format difference in the backend based on the browser used.

Simple form code to reproduce the issue

$form = [
  'date' => [
    '#type' => 'date',
    '#title' => 'Date',
    '#date_date_format' => 'd/m/Y',
  ],
  'submit' => [
    '#type' => 'submit',
    '#value' => 'Submit',
  ],
];

and watch the selected date value being sent as d/m/Y instead of Y-m-d in browsers using the polyfill (e.g Firefox), and correctly being sent as Y-m-d for browsers that don't (e.g. Chrome)

Proposed resolution

Use jQueryUI Datepicker altField / altFormat mecanism to have both a displayed field in the wanted format, but not submitted, and an hidden field with the correct value if the format used isn't the standard one.

Remaining tasks

- Review
- Figure out if there's a way to test the polyfill

API changes

- The displayed field will no longer have a name attribute ; it'll be used for the hidden input field instead. Themers that targetted a specific date field by its name will need to adapt their theming. That situation is unlikely though, because such an input required extra care in the validate / submit handlers to reformat the date based on the browser used

Members fund testing for the Drupal project. Drupal Association Learn more

Comments

DeFr created an issue. See original summary.

DeFr’s picture

Status: Active » Needs review
FileSize
2.43 KB

Uploading patch based on the proposition above.

jcisio’s picture

Status: Needs review » Needs work

The last submitted patch, 3: 2882346-3-Fix-date-polyfill-format-D8-3.x.patch, failed testing. View results

jcisio’s picture

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.