DateTimePicker: Fix Invalid Date errors in date field validation#75265
DateTimePicker: Fix Invalid Date errors in date field validation#75265
Conversation
) - Add validation for day and year inputs to prevent Invalid Date objects - Clamp out-of-bounds values to valid ranges (consistent with hour/minute behavior) - Revert to original value when field is cleared and blurred - Use key-based reset to sync InputControl display after clamping - Add comprehensive unit and integration tests for validation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Concerned about overengineering this. There may be easier ways to implement the validation so I'm open to hear alternatives. |
|
Size Change: +369 B (+0.01%) Total Size: 2.99 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
I wonder if we can have a more simplistic take on this: there are a bunch of places where we store the current date/time and we set it to something like currentDate ?? new Date(). Instead of just defaulting to a new Date() when currentDate is nullish, can't we just also set it to new Date() when currentDate is an Invalid Date? Which usually is as simple as this:
! ( currentDate instanceof Date && isFinite( currentDate ) )This should basically capture any invalid date/time scenarios, including the one where we haven't set a year or a day.
|
Yes I agree there's a route here to ship a simple targeted fix to the Core error. I still think we'll need to circle back to addrss some of the UX issues though. The picker provides a very poor experience. After all it's a scheduling interface that allows picking dates in the past. |
|
I agree, but I think handling incorrect dates properly without a WSOD and disallowing scheduling with past dates are two separate problems to solve. |
I agree - here's my simpler fix #75285 |
|
I've raised #75285 as a simpler alternative approach to this PR. It focuses on a minimal changeset by adding validation at the |
|
Thanks @getdave. Just to confirm: fixing the WSOD makes sense as a separate fix, but it still makes sense to pursue addressing the "shouldn't schedule with past dates" bug you mentioned, so this PR should still be valuable. |
What?
Fixes #75254 - Prevents the DateTimePicker from emitting "Invalid Date" strings when users clear or enter invalid values in day/year fields.
Why?
When users delete the Day or Year field values and then change the month, the TimePicker immediately emits an onChange with incomplete date data, creating an Invalid Date object. This propagates through the scheduling flow, causing "Scheduling failed. Invalid parameter(s): date" errors.
How?
Testing Instructions
Reproduce the original issue (should now be fixed)
Validation behavior scenarios to test
Unit tests
All scenarios above have corresponding unit tests. Run with:
npm run test:unit -- --testPathPattern="date-time"