Problem/Motivation

As originally reported in #2900251: "Date and time" Form API element allows to type 6 digit year component, The core date Form API element (and therefore, field widgets) allows values that don't match the min/max settings for the field.

This can lead to problems, such as users being able to enter more than 4 digits for the year. It should be limited to 4 digits.

Root cause

An empty "Date" field loads with no min/max year set.

\lib\Drupal\Core\Datetime\Element\Datetime::processDatetime() has this comment:

   * Optional properties include:
   *   - #date_year_range: A description of the range of years to allow, like
   *     '1900:2050', '-3:+3' or '2000:+3', where the first value describes the
   *     earliest year and the second the latest year in the range. A year
   *     in either position means that specific year. A +/- value describes a
   *     dynamic value that is that many years earlier or later than the current
   *     year at the time the form is displayed. Used in jQueryUI datepicker year
   *     range and HTML5 min/max date settings. Defaults to '1900:2050'.


When this element loads and is empty / doesn't have a defaul value this section of code is never hit:

      // Adds the HTML5 date attributes.
      if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
        $html5_min = clone($date);
        $range = static::datetimeRangeYears($element['#date_year_range'], $date);
        $html5_min->setDate($range[0], 1, 1)->setTime(0, 0, 0);
        $html5_max = clone($date);
        $html5_max->setDate($range[1], 12, 31)->setTime(23, 59, 59);

        $extra_attributes += array(
          'min' => $html5_min->format($date_format, $format_settings),
          'max' => $html5_max->format($date_format, $format_settings),
        );
      }

Date fields that have a value may set min max year to unwanted values

When the value of a date is set either via the default value or a saved date then the max min is calculated and set. This defaults to 1900-2050 (hard coded). However if the field year is set to a time before 1900 or above 2050 then this date is used _forever_ as the max or min value of that field. For example:
Error for min year
I can enter 1776-07-04 as a date in an empty element, and then this year gets used as the min year on the element instead of the range defined on the element (which is by design per a comment in DateElementBase::datetimeRangeYears()), but then you cant edit the year to be less than this (E.g. 1775) ever again. Same goes if you set the date to 2060 you can't then set it to 2062 (And worse there is no error shown on Chrome for this situation, it just silently fails).

Proposed resolution

  • Add the date_range_yearsconfiguration setting to the field edit form
    • If there is no data in this fields storage default to the current settings for date_range_years (1900-2050)
    • If there is data in this fields storage default to blank (?) for backwards capacitity
    • Allow the date_range_years config setting to be modified, validated and saved for this field
    • Add a description to the date_range_years field that if the field is left blank it will not add max-min to field at all
    • Perhaps this ticket covers some of that work? https://www.drupal.org/project/drupal/issues/2836054
  • The form element on the form display, If date_range_years is not empty should:
    • Use the date_range_years setting saved in the config.
    • Add the min/max elements even when the value on the element is empty.
  • Update code/docs to explain the new features when '#date_year_range' === NULL?
  • Show an error when the max value is exceeded (is this from the browser?)
  • Validate the date/range year submission from the widget [3269890]

Remaining tasks

  • Fix up patch of deprecation
  • Review
  • Commit

User interface changes

NA

API changes

NA

Data model changes

NA

Comments

mpdonadio created an issue. See original summary.

mpdonadio’s picture

I am a little worried about BC beyond the field widgets, too, for those using just the form elements. I'll post a test-only patch that demonstrates these problems sometime soon.

mpdonadio’s picture

Status: Active » Needs review
StatusFileSize
new4.23 KB

Demo to demonstrate the problem. Left this in WTB b/c that is where all of the other element tests are right now.

Status: Needs review » Needs work

The last submitted patch, 3: 2840220-03-demo.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mpdonadio’s picture

Version: 8.3.x-dev » 8.4.x-dev
Status: Needs work » Needs review
Issue tags: +DrupalCampNJ2017
StatusFileSize
new4.34 KB
new5.62 KB
new2.63 KB

Thoughts?

The last submitted patch, 6: 2840220-test-only.patch, failed testing.

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.

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

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

osab’s picture

https://www.drupal.org/files/issues/2840220-06.patch - didn't help me (D8.4.5, php7.1)
I checked Data and Data range fields.

Version: 8.6.x-dev » 8.7.x-dev

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

zimphire’s picture

StatusFileSize
new6.09 KB
zimphire’s picture

StatusFileSize
new5.69 KB

Now for an actual git patch. Sorry :(

The last submitted patch, 12: 2840220-07.patch, failed testing. View results

matsbla’s picture

I tested patch in #13, but I still experience the problem.

I have a datefield filter in views and I've pre-set the deafult value to: '1830-01-01 00:00:00'
I then apply the patch, run cron and clear cache.

Still, after, I get min="1830-01-01" inside my input element.

matsbla’s picture

Component: forms system » datetime.module
mpdonadio’s picture

Issue tags: +DrupalCampNJ2019
StatusFileSize
new873 bytes
new5.69 KB

Ok, reroll to convert the test to BTB.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Status: Needs review » Needs work

Just some tiny nits and a question/comment below.

  1. +++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php
    @@ -242,12 +242,17 @@ public static function processDatetime(&$element, FormStateInterface $form_state
    +      $extra_attributes += array(
    

    Nit: needs short array syntax

  2. +++ b/core/modules/system/tests/src/Functional/Form/DatetimeTest.php
    @@ -0,0 +1,57 @@
    +  protected $profile = 'testing';
    

    This shouldn't be necessary as it's the same as the one declared by BrowserTestBase.

  3. +++ b/core/modules/system/tests/src/Functional/Form/DatetimeTest.php
    @@ -0,0 +1,57 @@
    +    $this->assertFieldByXPath('//div[@id="edit-datetime"]//input[@type="date" and @min="1900-01-01" and @max="2050-12-31"]', '', 'Found input[@type="date" and @min="1900-01-01" and @max="2050-12-31"]');
    ...
    +    $this->assertFieldByXPath('//div[@id="edit-datetime"]//input[@type="date" and @min="1900-01-01" and @max="2050-12-31"]', '2016-12-31', 'Found input[@type="date" and @min="1900-01-01" and @max="2050-12-31"]');
    ...
    +    $this->assertFieldByXPath('//div[@id="edit-datetime"]//input[@type="date" and @min="1776-01-01" and @max="2050-12-31"]', '1776-07-14', 'Found input[@type="date" and @min="1776-01-01" and @max="2050-12-31"]');
    

    It's not clear to me where these ranges are coming from, and how they are being set in FormTestDatetimeForm. Can some clarifying comments be added either to the form class or to the test itself?

  4. +++ b/core/modules/system/tests/src/Functional/Form/DatetimeTest.php
    @@ -0,0 +1,57 @@
    +    // Post values w/in 1900-2050 and check min/max.
    

    Tiny nit: Can you spell out within here?

cainaru’s picture

StatusFileSize
new1.25 MB
new170.11 KB

I can enter 1776-07-04 as a date in an empty element, and then this year gets used as the min year on the element instead of the range defined on the element (which is by design per a comment in DateElementBase::datetimeRangeYears()), but then you cant edit the year to be less than this ever again.

We just ran into this issue. A student accidentally entered 10/01/1850 instead of 10/01/1848 and saved the node. The student then tried to edit the node again to correct the date, but received an error stating "Value must be greater than or equal to 1850-01-01" or "Value must be 01/01/1850 or later" (see images below).

We haven't applied the patch from #17 yet, but the following work-around was successful (in case anyone reading this is unable to apply the patch yet but needs some sort of temporary solution):

  • In the node edit form, hover your cursor over the erroneous value in the date field until an "x" next to the date appears.
  • Click the "x" to clear the value. The value should now be something like mm/dd/yyyy instead of the incorrect date.
  • Save the node.
  • Edit the node again, and enter the correct value into the date field.
  • Save the node again. Upon saving the node with the corrected date, you should not be receiving the error about "Value must be XYZ or later".

A screenshot of a node edit form on a mobile device in which the user attempts to enter October 1, 1848 as a value for an End Date field, but receives an error which states Value must be greater than or equal to 1850-01-01.

A screenshot of a node edit form on a desktop computer in which the user attempts to enter 01/01/1848 as a value for an End Date field, but receives an error which states Value must be 01/01/1850 or later.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Postponed

Believe this is a duplicate of https://www.drupal.org/project/drupal/issues/2900251 anyone willing to confirm?

scottop’s picture

Believe this is a duplicate of https://www.drupal.org/project/drupal/issues/2900251 anyone willing to confirm?

No, this is a different issue.

There is a workaround— add the Datetime Extras module. The problem is not in the data, but in the validation that the entry form imposes. The Datetime Extras module allows you to set the allowable date range for a valid entry, instead of getting the HTML 4 default 0f 1900–2050.

smustgrave’s picture

Status: Postponed » Needs work

For #19

ravi.shankar’s picture

StatusFileSize
new5.65 KB
new1.78 KB

Addressed point 1, 2 and 4 of comments #19, still needs work for point number 3 of comment #19.

smustgrave’s picture

Ignoring #31 because of the deprecation errors.

Regarding #19.3 Also not understanding where the data ranges are coming from.

Also the solution here looks pretty similar to https://www.drupal.org/project/drupal/issues/2900251 which is currently marked RTBC.

So if https://www.drupal.org/project/drupal/issues/2900251 isn't addressing this issue maybe this is postponed until that is merged?

Also needs an IS update to be more clear.

ravi.shankar’s picture

StatusFileSize
new5.73 KB
new2.28 KB

Trying to fix tests of patch #31 and fixed Drupal CS issues.

ravi.shankar’s picture

StatusFileSize
new5.78 KB
new692 bytes
new2.35 KB

Fixed failed tests of patch #33 and added test only patch this might help.

Still needs work for #32.

smustgrave’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update
StatusFileSize
new4.38 KB
new3.4 KB
new5.65 KB

Updated the IS slightly about what's left todo.

Finally figured out the dates.

First set are from when the tests were created initially
Second set I think is around US. 1776 at least is. But it's before the min 1900
Third set I believe is just a random set after 2050

smustgrave’s picture

Status: Needs review » Needs work

Tests need to be refined

smustgrave’s picture

StatusFileSize
new1.44 KB
new5.1 KB
new2.71 KB

Before the tests get worked on how exactly is this suppose to work?
I added a date field to a content type and min/max gets set to 1900-01-01 and 2050-12-31 with this patch. Previously it was getting no min or max.
How am I suppose to set a 1756 time? Is that covered in https://www.drupal.org/project/drupal/issues/2942828

Pulling the tests from https://www.drupal.org/project/drupal/issues/290025 to test the min/max variable at least.
Removing the old ones until

smustgrave’s picture

Status: Needs work » Needs review

The last submitted patch, 37: 2840220-37-tests-only.patch, failed testing. View results

quietone credited alexpott.

quietone credited dww.

quietone credited mstrelan.

quietone credited pameeela.

quietone’s picture

smustgrave closed #2900251: "Date and time" Form API element allows to type 6 digit year component as a duplicate. I have added it as a related issue and I am moving credit.

quietone’s picture

This could use an issue title update. What 'buggy' behavior is being fixed here?

smustgrave’s picture

@quietone what about "Datetime missing min/max attributes"
This issue fixes a problem where min and max attributes were not be added to the date field.

In addition, I can enter 1776-07-04 as a date in an empty element

The original issue talks about this but I don't know how they set that up with this patch.

Any suggestion on how best to proceed?

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jaime@gingerrobot.com’s picture

jaime@gingerrobot.com’s picture

Title: Datetime min/max attributes are buggy » Datetime should respect but not require min/max attributes
jaime@gingerrobot.com’s picture

Issue summary: View changes
StatusFileSize
new20.17 KB
jaime@gingerrobot.com’s picture

Issue summary: View changes
jaime@gingerrobot.com’s picture

Issue summary: View changes
jaime@gingerrobot.com’s picture

gaurav-mathur’s picture

StatusFileSize
new35.95 KB

This issue is already resolve in the latest version , Adding screenshot.

Thankyou

prasanth_kp’s picture

StatusFileSize
new14.45 KB

#37 Patch Applied on 10.1.x-dev and it fixes the issue.

mark_fullmer’s picture

Title: Datetime should respect but not require min/max attributes » "Date and time" Form API element allows entry beyond min/max values
Issue summary: View changes
mark_fullmer’s picture

This could use an issue title update. What 'buggy' behavior is being fixed here?

I've updated the title to capture the bugginess: "Date and time" Form API element allows entry beyond min/max values

feyp’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Looking at the proposed resolution in the issue summary, it seems that the patch only implements the "Add the min/max elements even when the value on the element is empty." part. All the rest is missing, which is a lot. So this is very far from ready, unfortunately.

If this change breaks the functionality of the date field for working with dates outside the default range, then I think we also can't just rescope this issue and do the rest in a follow-up. We must at least ensure that people who use the field with existing data outside of the default range can still use the date field until the follow-up is implemented.

The issue linked from the top of the issue summary mentions that it is annoying for editors when they accidentally enter a 6 digit year instead of entering a 4 digit year and then the first 2 digits of the next date component. I tested the patch in both current Firefox and Chromium on Linux and although min and max are now set to the default date range, I can still enter 6 digit years. Only when I then try to submit the form, instead of getting an error from Form API about the required date format, I now get a browser validation error (which will also enforce that the date is within the date range specified). I'm not sure how to fix this, maybe this would need some Javascript?

I would also suggest to use an ordered list instead of an unordered list for the proposed resolution in the issue summary, as that will make it easier to refer to individual items in comments.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Dimm’s picture

https://www.drupal.org/project/datetime_more
The "Datetime More Widget" module allows the user to independently specify the minimum and maximum year 0001-9999.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.