Skip to content

Commit 355edc2

Browse files
committed
feat(pat-validation): Emit pat-update events with actions invalid and valid after validation checks on inputs.
1 parent e544947 commit 355edc2

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/pat/validation/validation.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Pattern extends BasePattern {
113113
}
114114

115115
// In any case, clear the custom validity first.
116-
this.set_error({ input: input, msg: "" });
116+
this.set_error({ input: input, msg: "", skip_event: true });
117117
const validity_state = input.validity;
118118

119119
if (event?.submitter?.hasAttribute("formnovalidate")) {
@@ -315,7 +315,11 @@ class Pattern extends BasePattern {
315315
input_options.message.datetime
316316
) {
317317
this.set_error({ input: input, msg: input_options.message.datetime });
318+
} else {
319+
// Still an error, but here we need to call `emit_update` separately
320+
this.emit_update("invalid");
318321
}
322+
319323
}
320324

321325
if (event?.type === "submit") {
@@ -327,7 +331,7 @@ class Pattern extends BasePattern {
327331
this.set_error_message(input);
328332
}
329333

330-
set_error({ input, msg, attribute = null, min = null, max = null }) {
334+
set_error({ input, msg, attribute = null, min = null, max = null, skip_event = false }) {
331335
// Replace some variables, as like validate.js
332336
if (attribute) {
333337
msg = msg.replace(/%{attribute}/g, attribute);
@@ -346,9 +350,12 @@ class Pattern extends BasePattern {
346350
// (e.g. styled date input).
347351
input[KEY_ERROR_MSG] = msg;
348352

353+
if (!skip_event) {
354+
this.emit_update("invalid");
355+
}
349356
}
350357

351-
remove_error(input, all_of_group = false) {
358+
remove_error(input, all_of_group = false, skip_event = false) {
352359
// Remove error message and related referencesfrom input.
353360

354361
let inputs = [input];
@@ -371,6 +378,10 @@ class Pattern extends BasePattern {
371378
}
372379
}
373380
}
381+
382+
if (!skip_event) {
383+
this.emit_update("valid");
384+
}
374385
}
375386

376387
set_error_message(input) {

src/pat/validation/validation.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,41 @@ describe("pat-validation", function () {
503503
expect(el.querySelectorAll("em.warning").length).toBe(1);
504504
});
505505

506+
it("1.21 - Emits an update event when the validation state changes", async function () {
507+
document.body.innerHTML = `
508+
<form class="pat-validation">
509+
<input name="name" required>
510+
</form>
511+
`;
512+
const el = document.querySelector(".pat-validation");
513+
const inp = el.querySelector("[name=name]");
514+
515+
const instance = new Pattern(el);
516+
await events.await_pattern_init(instance);
517+
518+
let event;
519+
el.addEventListener("pat-update", (e) => {
520+
event = e;
521+
});
522+
523+
inp.value = "";
524+
inp.dispatchEvent(events.change_event());
525+
await utils.timeout(1); // wait a tick for async to settle.
526+
527+
expect(el.querySelectorAll("em.warning").length).toBe(1);
528+
expect(event.detail.pattern).toBe("validation");
529+
expect(event.detail.dom).toBe(el);
530+
expect(event.detail.action).toBe("invalid");
531+
532+
inp.value = "okay";
533+
inp.dispatchEvent(events.change_event());
534+
await utils.timeout(1); // wait a tick for async to settle.
535+
536+
expect(event.detail.pattern).toBe("validation");
537+
expect(event.detail.dom).toBe(el);
538+
expect(event.detail.action).toBe("valid");
539+
});
540+
506541
it("2.1 - validates required inputs", async function () {
507542
document.body.innerHTML = `
508543
<form class="pat-validation">

0 commit comments

Comments
 (0)