Skip to content

Commit 39249b2

Browse files
committed
fix(pat validation): Also validate date/time inputs when the value is empty.
This clears eventual validation errors when the date input is cleared.
1 parent 0fb2359 commit 39249b2

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/pat/validation/validation.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,7 @@ export default Base.extend({
138138
const msg = input_options.message.date || input_options.message.datetime;
139139

140140
let not_after;
141-
let not_before;
142141
let not_after_el;
143-
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-
}
149-
const date = new Date(input.value);
150-
if (isNaN(date)) {
151-
// Not a valid date, return.
152-
// Should not happen or input only partially typed in.
153-
return;
154-
}
155142
if (input_options.not.after) {
156143
if (utils.is_iso_date_time(input_options.not.after, true)) {
157144
not_after = new Date(input_options.not.after);
@@ -166,6 +153,9 @@ export default Base.extend({
166153
// Use null if no valid date.
167154
not_after = isNaN(not_after) ? null : not_after;
168155
}
156+
157+
let not_before;
158+
let not_before_el;
169159
if (input_options.not.before) {
170160
if (utils.is_iso_date_time(input_options.not.before, true)) {
171161
not_before = new Date(input_options.not.before);
@@ -180,11 +170,22 @@ export default Base.extend({
180170
// Use null if no valid date.
181171
not_before = isNaN(not_before) ? null : not_before;
182172
}
183-
if (not_after && date > not_after) {
184-
this.set_validity({ input: input, msg: msg });
185-
} else if (not_before && date < not_before) {
186-
this.set_validity({ input: input, msg: msg });
173+
174+
if (
175+
input.value &&
176+
utils.is_iso_date_time(input.value, true) &&
177+
!isNaN(new Date(input.value))
178+
) {
179+
// That's 1 valid date!
180+
const date = new Date(input.value);
181+
182+
if (not_after && date > not_after) {
183+
this.set_validity({ input: input, msg: msg });
184+
} else if (not_before && date < not_before) {
185+
this.set_validity({ input: input, msg: msg });
186+
}
187187
}
188+
188189
// always check the other input to clear/set errors
189190
!stop && // do not re-check when stop is set to avoid infinite loops
190191
not_after_el &&

src/pat/validation/validation.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,20 @@ describe("pat-validation", function () {
864864
inp_start.dispatchEvent(events.change_event());
865865
await utils.timeout(1); // wait a tick for async to settle.
866866
expect(el.querySelectorAll("em.warning").length).toBe(0);
867+
868+
// Violate the constraint again...
869+
inp_start.value = "2020-10-11";
870+
inp_start.dispatchEvent(events.change_event());
871+
inp_end.value = "2020-10-10";
872+
inp_end.dispatchEvent(events.change_event());
873+
await utils.timeout(1); // wait a tick for async to settle.
874+
expect(el.querySelectorAll("em.warning").length).toBe(2);
875+
876+
// Clearing one of the optional values should clear all errors.
877+
inp_start.value = "";
878+
inp_start.dispatchEvent(events.change_event());
879+
await utils.timeout(1); // wait a tick for async to settle.
880+
expect(el.querySelectorAll("em.warning").length).toBe(0);
867881
});
868882

869883
it("5.6 - doesn't validate empty optional dates", async function () {

0 commit comments

Comments
 (0)