Skip to content

Commit 0fb2359

Browse files
committed
fix(pat validation): Fix issue where Google Chrome would interpret an invalid date value like "ok-1" as a valid date.
1 parent d9fcfac commit 0fb2359

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

src/pat/validation/validation.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,40 +141,40 @@ export default Base.extend({
141141
let not_before;
142142
let not_after_el;
143143
let not_before_el;
144+
145+
if (!utils.is_iso_date_time(input.value, true)) {
146+
// Not a valid date at all, return.
147+
return;
148+
}
144149
const date = new Date(input.value);
145150
if (isNaN(date)) {
151+
// Not a valid date, return.
146152
// Should not happen or input only partially typed in.
147153
return;
148154
}
149155
if (input_options.not.after) {
150-
// Handle value as date.
151-
not_after = new Date(input_options.not.after);
152-
if (isNaN(not_after)) {
156+
if (utils.is_iso_date_time(input_options.not.after, true)) {
157+
not_after = new Date(input_options.not.after);
158+
} else {
153159
// Handle value as selector
154160
not_after_el = document.querySelector(input_options.not.after);
155-
not_after = not_after_el?.value;
156-
not_after =
157-
not_after &&
158-
new Date(
159-
document.querySelector(input_options.not.after).value
160-
);
161+
not_after = not_after_el?.value
162+
? new Date(not_after_el?.value)
163+
: undefined;
161164
}
162165

163166
// Use null if no valid date.
164167
not_after = isNaN(not_after) ? null : not_after;
165168
}
166169
if (input_options.not.before) {
167-
// Handle value as date.
168-
not_before = new Date(input_options.not.before);
169-
if (isNaN(not_before)) {
170+
if (utils.is_iso_date_time(input_options.not.before, true)) {
171+
not_before = new Date(input_options.not.before);
172+
} else {
170173
// Handle value as selector
171174
not_before_el = document.querySelector(input_options.not.before);
172-
not_before = not_before_el?.value;
173-
not_before =
174-
not_before &&
175-
new Date(
176-
document.querySelector(input_options.not.before).value
177-
);
175+
not_before = not_before_el?.value
176+
? new Date(not_before_el?.value)
177+
: undefined;
178178
}
179179

180180
// Use null if no valid date.

src/pat/validation/validation.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,34 @@ describe("pat-validation", function () {
992992
expect(el.querySelectorAll("em.warning").length).toBe(0);
993993
});
994994

995+
it("5.9 - Do not interpret ``ok-1`` as a valid date.", async function () {
996+
// This issue popped up in Chrome but not in Firefox.
997+
// A date like ``ok-1`` was interpreted as ``2000-12-31T23:00:00.000Z``.
998+
// Explicitly checking for a valid ISO 8601 date fixes this.
999+
1000+
document.body.innerHTML = `
1001+
<form class="pat-validation">
1002+
<input
1003+
type="date"
1004+
name="date"
1005+
data-pat-validation="message-date: Wong date!; not-after: ok-1"
1006+
/>
1007+
</form>
1008+
`;
1009+
1010+
const el = document.querySelector(".pat-validation");
1011+
const inp = el.querySelector("[name=date]");
1012+
1013+
new Pattern(el);
1014+
await utils.timeout(1); // wait a tick for async to settle.
1015+
1016+
inp.value = "2022-01-01";
1017+
inp.dispatchEvent(events.change_event());
1018+
await utils.timeout(1); // wait a tick for async to settle.
1019+
1020+
expect(el.querySelectorAll("em.warning").length).toBe(0);
1021+
});
1022+
9951023
it("6.1 - validates radio buttons", async function () {
9961024
document.body.innerHTML = `
9971025
<form class="pat-validation"

0 commit comments

Comments
 (0)