(couldn't find a daterange component so filing under datetime.module)

Currently in the daterange field you can select a "to" date that is before the "from" date.

This is checked and indicated with an error message:

It would be better to prevent people from entering an earlier date in the first place.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yoroy created an issue. See original summary.

magpie5212’s picture

What if the user spots the need for a date change and decides to change the 'to' date first on the way to changing the 'from' date?

yoroy’s picture

Yes, that needs figuring out too :)

jhedstrom’s picture

We could also greatly improve on that error message that is currently displayed...

csg’s picture

I agree with making "to" dates that are before the "from" date disabled. In my opinion the likelihood of someone spotting the need for a date change and starts with the "to" date is small, and the level of inconvenience they would experience in this case is pretty insignificant. I would gladly sacrifice it for ensuring that invalid date ranges don't even make it to the server, they get caught in the browser. It's better for the user (faster feedback) and better for the site owner (less requests to handle by the server).

The other option I see is that we automatically update the "from" date when the user picks a preceding "to" date so the two dates are at least equal, but I think from a user experience standpoint automatically modifying incorrect user input is worse than preventing incorrect user input, at least in cases like this, when we don't know what to modify it to because we have no clue about what the user wants. I mean, it's pretty unlikely that the user wants to enter the same date to both fields, so this modification would be a correction for Drupal but not for the users.

Finally, the error message could be improved too. I simply suggest to remove the confusing part (that ironically the author probably meant as clarification):
"The end date cannot be before the start date"

yoroy’s picture

Ways I see this handled elsewhere:

1. on selection of a "from" date, disable that day and all before it so as to make them not selectable
2. let people choose a "to" date and if that is earlier than the "from" date, then automatically change the "before" date to 1 day before "to".

mpdonadio’s picture

Thanks for the input. When the field is configured for date+time, then you definitely will have people using the same date but different times. Also, it has come up where people are using the date-only flavor of the field and using the same date for start and end.

And for anyone trying to figure out a patch, we have a few complications...

- Browsers with non-HTML date/time inputs get the polyfill.
- If configured as a field, we know how the element is configured out of the box, but if we solve this at the element level (instead of the widget) and not the field, then there are some weird combinations we need to consider.

nod_’s picture

Issue tags: +JavaScript

Might be taboo but how about automatically reordering the dates on the php side so it's always earlier date as 1st value + some text in the widget config to warn of the behavior? (and possibly a warning message saying dates were swapped after save)

xjm’s picture

Issue summary: View changes
FileSize
39.4 KB

Looking at #2 -- tricky indeed. On various datetime range inputs on my phone, for example, if I make the start date/time later than the end date/time, it automatically advances the end date/time to be the same as the start date/time. Sometimes this is helpful, and other times it's annoying because I lose my intentional input for all the end date year, month, day, and time just because I accidentally misclicked on (e.g.) one year higher than I meant for the start date.

Might be taboo but how about automatically reordering the dates on the php side so it's always earlier date as 1st value + some text in the widget config to warn of the behavior? (and possibly a warning message saying dates were swapped after save)

I do not think this is a good idea. :) Better a validation error than unexpectedly/magically trying to change saved data in this way. This could have all kinds of unexpected results.

I think part of what @yoroy was suggesting was that the widget should show you what the start date you picked was. Lots of calendar picker widgets have thingies that look like:

That's the hipmunk daterange selector widget. If I click on the start date field and click July 27, it moves the forward arrow but also blanks out the end date so I have to reselect it. But that's not what our datepicker does.

Complicated problem, for sure.

mpdonadio’s picture

So, are we going to entertain the option of abandoning the HTML5 inputs in favor of a JS library? That could have some ripple effects into the Datetime and Timestamp widgets.

xjm’s picture

Sorry, #9 wasn't a recommendation or anything; I was just trying to illustrate different ways that different apps solve #2.

The minimum fix would probably be to dynamically set the minimum allowed end date value to the same as the start date. That way, we don't have to worry about edgecases as much, and presumably we wouldn't have to do anything special with the polyfill to support that.

mpdonadio’s picture

I think it is worth discussing, potentially as a f/up to this. I think the native HTML5 elements are better, but the user expectations are mixed. A lot think that the popup is coming from core, but only when a browser doesn't support native HTML5 elements. We have had bunches of issues about this, and in particular the quirk with the date formats. There is a decent argument about a standard date/time picker across all browsers. But, you could also argue that this should be deferred to contrib.

The minimum fix would probably be to dynamically set the minimum allowed end date value to the same as the start date.

I think that really is the best starting point, unless there is better guidance from Google Calendar, OSX Calendar, and Outlook. We just have to remember that date+time is a compound element.

I think the general pseudo code is

start.onchange => end.min = start.value; if end.value < start.value then end.value = start.value

and we can handle this with the min attributes, maybe add a dynamic message if we bump the end value. But I'm not really sure we can really enforce the min time strictly with the attribute.

Need to track down my UI pattern books...

yoroy’s picture

The minimum fix would probably be to dynamically set the minimum allowed end date value to the same as the start date.

Agreed. Lets be pragmatic here and do what we reasonably can.

mpdonadio’s picture

Status: Active » Needs review
Issue tags: +Needs manual testing, +Needs tests
FileSize
4.54 KB

Here is a starting point to play with. I think the patch has the proper list of remaining work as @todos.

Added Needs manual testing for the time being, but we will eventually want JSTB test coverage.

timisoreana’s picture

FileSize
25.45 KB
26.11 KB

I've tested #14 patch.

  1. When user edit, he can add an end date < start date.
  2. Steps:

    Add normal dates on calendar (start date < end date) and save it
    open the node for editing
    change end date

    Actual result:all dates are available for end date and user can select end dates < start date
    End date on editing
    Expected result: should be available only end dates >= start date

    Note:if user change start date after second step, then is working as expected

  3. Error message is not enough exactly, when user put the wrong time
    wrong error message for time
    Maybe will be better:"The [field_name] end date(time) cannot be before the start date(time)" for all cases (date and time)?
mpdonadio’s picture

#15, thanks for testing this out. That is a nice catch, and a good suggestion. We are going to handle the error messages in #2895055: Edit the date range error messages; this issue is really just about adding some client-side JS validation / handling to try to prevent a round-trip for validation.

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.

jhedstrom’s picture

Status: Needs review » Needs work

Trying to clear out the NR queue, so bumping to NW for all the @todos and the tests.

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.

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.

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.

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.

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.

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.