@ajosecortes/form-guard 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +417 -0
- package/dist/FormGuard.d.ts +51 -0
- package/dist/dom.d.ts +11 -0
- package/dist/form-guard.cjs +2 -0
- package/dist/form-guard.cjs.map +1 -0
- package/dist/form-guard.js +580 -0
- package/dist/form-guard.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/locales/en.d.ts +3 -0
- package/dist/locales/index.d.ts +2 -0
- package/dist/rules.d.ts +19 -0
- package/dist/types.d.ts +102 -0
- package/package.json +34 -0
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
var v = Object.defineProperty;
|
|
2
|
+
var b = (r, e, t) => e in r ? v(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
+
var u = (r, e, t) => b(r, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
const y = {
|
|
5
|
+
required: "This field is required",
|
|
6
|
+
email: "Please enter a valid email address",
|
|
7
|
+
minLength: "Must be at least {0} characters",
|
|
8
|
+
maxLength: "Must be no more than {0} characters",
|
|
9
|
+
number: "Must be a number",
|
|
10
|
+
integer: "Must be an integer",
|
|
11
|
+
min: "Must be at least {0}",
|
|
12
|
+
max: "Must be no more than {0}",
|
|
13
|
+
pattern: "Invalid format",
|
|
14
|
+
sameAs: "Must match {0}",
|
|
15
|
+
files: "Please select at least one file",
|
|
16
|
+
minFiles: "Please select at least {0} files",
|
|
17
|
+
maxFiles: "Please select at most {0} files",
|
|
18
|
+
strongPassword: "Password must contain uppercase, lowercase, digit, and special character"
|
|
19
|
+
};
|
|
20
|
+
function d(r, e, t) {
|
|
21
|
+
const s = (r == null ? void 0 : r.errorContainer) ?? (e == null ? void 0 : e.errorContainer);
|
|
22
|
+
if (!s) return null;
|
|
23
|
+
if (s instanceof HTMLElement) return s;
|
|
24
|
+
const i = s;
|
|
25
|
+
if (t != null && t.closest) {
|
|
26
|
+
const n = t.closest(i);
|
|
27
|
+
if (n) return n;
|
|
28
|
+
}
|
|
29
|
+
return document.querySelector(i);
|
|
30
|
+
}
|
|
31
|
+
function h(r, e, t) {
|
|
32
|
+
const s = (e == null ? void 0 : e.errorClass) ?? (t == null ? void 0 : t.errorClass) ?? "form-guard-error", i = (e == null ? void 0 : e.successClass) ?? (t == null ? void 0 : t.successClass) ?? "form-guard-success", n = (t == null ? void 0 : t.pendingClass) ?? "form-guard-pending";
|
|
33
|
+
r.classList.remove(s), r.classList.remove(i), r.classList.remove(n), r.removeAttribute("aria-invalid"), r.removeAttribute("aria-describedby");
|
|
34
|
+
const a = d(e, t, r);
|
|
35
|
+
a && (a.innerHTML = "", a.setAttribute("role", "alert"));
|
|
36
|
+
}
|
|
37
|
+
function f(r, e, t) {
|
|
38
|
+
const s = (e == null ? void 0 : e.errorClass) ?? (t == null ? void 0 : t.errorClass) ?? "form-guard-error", i = (e == null ? void 0 : e.successClass) ?? (t == null ? void 0 : t.successClass) ?? "form-guard-success", n = (t == null ? void 0 : t.pendingClass) ?? "form-guard-pending";
|
|
39
|
+
r.classList.remove(s), r.classList.remove(i), r.classList.add(n), r.removeAttribute("aria-invalid"), r.removeAttribute("aria-describedby");
|
|
40
|
+
const a = d(e, t, r);
|
|
41
|
+
a && (a.innerHTML = '<span class="form-guard-message form-guard-pending-text">Checking...</span>', a.setAttribute("role", "status"), a.setAttribute("aria-live", "polite"));
|
|
42
|
+
}
|
|
43
|
+
function E(r, e, t, s) {
|
|
44
|
+
const i = (t == null ? void 0 : t.errorClass) ?? (s == null ? void 0 : s.errorClass) ?? "form-guard-error", n = (t == null ? void 0 : t.successClass) ?? (s == null ? void 0 : s.successClass) ?? "form-guard-success", a = (s == null ? void 0 : s.pendingClass) ?? "form-guard-pending";
|
|
45
|
+
r.classList.add(i), r.classList.remove(n), r.classList.remove(a), r.setAttribute("aria-invalid", "true");
|
|
46
|
+
const o = d(t, s, r);
|
|
47
|
+
if (o) {
|
|
48
|
+
const c = `form-guard-error-${e.field}`;
|
|
49
|
+
o.innerHTML = `<span id="${c}" class="form-guard-message">${A(e.message)}</span>`, o.setAttribute("role", "alert"), r.setAttribute("aria-describedby", c);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function L(r, e, t) {
|
|
53
|
+
const s = (e == null ? void 0 : e.errorClass) ?? (t == null ? void 0 : t.errorClass) ?? "form-guard-error", i = (e == null ? void 0 : e.successClass) ?? (t == null ? void 0 : t.successClass) ?? "form-guard-success", n = (t == null ? void 0 : t.pendingClass) ?? "form-guard-pending";
|
|
54
|
+
r.classList.remove(s), r.classList.add(i), r.classList.remove(n), r.removeAttribute("aria-invalid"), r.removeAttribute("aria-describedby");
|
|
55
|
+
const a = d(e, t, r);
|
|
56
|
+
a && (a.innerHTML = "");
|
|
57
|
+
}
|
|
58
|
+
function g(r) {
|
|
59
|
+
for (const { element: e, errors: t } of r)
|
|
60
|
+
if (t.length > 0) {
|
|
61
|
+
e.focus();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function A(r) {
|
|
66
|
+
const e = document.createElement("div");
|
|
67
|
+
return e.textContent = r, e.innerHTML;
|
|
68
|
+
}
|
|
69
|
+
function m(r, e) {
|
|
70
|
+
return r.startsWith("#") || r.startsWith(".") || r.includes("[") ? e.querySelector(r) : e.querySelector(`[name="${r}"]`);
|
|
71
|
+
}
|
|
72
|
+
function p(r, e) {
|
|
73
|
+
return r.startsWith("#") || r.startsWith(".") || r.includes("[") ? Array.from(
|
|
74
|
+
e.querySelectorAll(r)
|
|
75
|
+
) : Array.from(
|
|
76
|
+
e.querySelectorAll(
|
|
77
|
+
`[name="${r}"]`
|
|
78
|
+
)
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
function F(r) {
|
|
82
|
+
return r instanceof HTMLInputElement && r.type === "file" ? r.files : r instanceof HTMLInputElement && (r.type === "checkbox" || r.type === "radio") ? r.checked : r instanceof HTMLSelectElement && r.multiple ? Array.from(r.selectedOptions).map((t) => t.value) : r.value;
|
|
83
|
+
}
|
|
84
|
+
function k(r) {
|
|
85
|
+
const e = [];
|
|
86
|
+
for (const t of r)
|
|
87
|
+
t instanceof HTMLInputElement && (t.type === "checkbox" || t.type === "radio") && t.checked && e.push(t.value);
|
|
88
|
+
if (r.length > 0) {
|
|
89
|
+
const t = r[0];
|
|
90
|
+
if (t instanceof HTMLInputElement && t.type === "radio")
|
|
91
|
+
return e.length > 0 ? e[0] : "";
|
|
92
|
+
}
|
|
93
|
+
return e;
|
|
94
|
+
}
|
|
95
|
+
function M(r, e) {
|
|
96
|
+
var t;
|
|
97
|
+
return e != null && e.label ? e.label : r.labels && r.labels.length > 0 ? ((t = r.labels[0].textContent) == null ? void 0 : t.trim()) ?? r.name : r.name || "field";
|
|
98
|
+
}
|
|
99
|
+
function l(r, e, t, s, i) {
|
|
100
|
+
return { code: r, message: e, field: t, rule: s, value: i, meta: {} };
|
|
101
|
+
}
|
|
102
|
+
class S {
|
|
103
|
+
constructor(e, t) {
|
|
104
|
+
u(this, "form");
|
|
105
|
+
u(this, "config");
|
|
106
|
+
u(this, "fields", /* @__PURE__ */ new Map());
|
|
107
|
+
u(this, "groups", /* @__PURE__ */ new Map());
|
|
108
|
+
u(this, "locale");
|
|
109
|
+
u(this, "successCallbacks", []);
|
|
110
|
+
u(this, "failCallbacks", []);
|
|
111
|
+
u(this, "validateCallbacks", []);
|
|
112
|
+
u(this, "boundSubmit", null);
|
|
113
|
+
u(this, "boundBlur", null);
|
|
114
|
+
u(this, "boundChange", null);
|
|
115
|
+
u(this, "destroyed", !1);
|
|
116
|
+
u(this, "handleSubmit", async (e) => {
|
|
117
|
+
if (this.config.mode !== "onSubmit") return;
|
|
118
|
+
e.preventDefault(), await this.validate() && this.form.submit();
|
|
119
|
+
});
|
|
120
|
+
if (this.config = {
|
|
121
|
+
mode: "onSubmit",
|
|
122
|
+
focusOnError: !0,
|
|
123
|
+
lockForm: !1,
|
|
124
|
+
collectAll: !1,
|
|
125
|
+
debounceMs: 0,
|
|
126
|
+
...t
|
|
127
|
+
}, this.locale = { ...y, ...t == null ? void 0 : t.locale }, typeof e == "string") {
|
|
128
|
+
const s = document.querySelector(e);
|
|
129
|
+
if (!s)
|
|
130
|
+
throw new Error(`FormGuard: form not found for selector "${e}"`);
|
|
131
|
+
this.form = s;
|
|
132
|
+
} else
|
|
133
|
+
this.form = e;
|
|
134
|
+
this.bindEvents();
|
|
135
|
+
}
|
|
136
|
+
addField(e, t, s) {
|
|
137
|
+
if (this.destroyed) return this;
|
|
138
|
+
const i = e, n = m(e, this.form);
|
|
139
|
+
if (!n)
|
|
140
|
+
return this.config.debug && console.warn(`FormGuard: field "${e}" not found`), this;
|
|
141
|
+
if (this.fields.set(i, {
|
|
142
|
+
field: n,
|
|
143
|
+
rules: [...t],
|
|
144
|
+
config: s ?? {},
|
|
145
|
+
errors: [],
|
|
146
|
+
validated: !1,
|
|
147
|
+
pending: !1,
|
|
148
|
+
abortController: null,
|
|
149
|
+
debounceTimer: null
|
|
150
|
+
}), this.config.mode === "onBlur" && n.addEventListener("blur", this.handleBlur(i)), this.config.mode === "onChange") {
|
|
151
|
+
const a = n instanceof HTMLInputElement && (n.type === "checkbox" || n.type === "radio") ? "change" : "input";
|
|
152
|
+
n.addEventListener(a, this.handleChange(i));
|
|
153
|
+
}
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
removeField(e) {
|
|
157
|
+
const t = this.fields.get(e);
|
|
158
|
+
return t && (this.cancelFieldValidation(t), h(t.field, t.config, this.config), t.field.removeEventListener("blur", this.handleBlur(e)), t.field.removeEventListener("input", this.handleChange(e)), t.field.removeEventListener("change", this.handleChange(e))), this.fields.delete(e), this;
|
|
159
|
+
}
|
|
160
|
+
addGroup(e, t, s) {
|
|
161
|
+
if (this.destroyed) return this;
|
|
162
|
+
const i = p(e, this.form);
|
|
163
|
+
if (i.length === 0)
|
|
164
|
+
return this.config.debug && console.warn(`FormGuard: no elements found for group "${e}"`), this;
|
|
165
|
+
if (this.groups.set(e, {
|
|
166
|
+
elements: i,
|
|
167
|
+
rules: [...t],
|
|
168
|
+
config: s ?? {},
|
|
169
|
+
errors: [],
|
|
170
|
+
validated: !1,
|
|
171
|
+
pending: !1,
|
|
172
|
+
abortController: null,
|
|
173
|
+
debounceTimer: null
|
|
174
|
+
}), this.config.mode === "onBlur" || this.config.mode === "onChange")
|
|
175
|
+
for (const n of i) {
|
|
176
|
+
n.addEventListener("blur", this.handleGroupEvent(e));
|
|
177
|
+
const a = n instanceof HTMLInputElement && (n.type === "checkbox" || n.type === "radio") ? "change" : "input";
|
|
178
|
+
n.addEventListener(a, this.handleGroupEvent(e));
|
|
179
|
+
}
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
removeGroup(e) {
|
|
183
|
+
const t = this.groups.get(e);
|
|
184
|
+
if (t) {
|
|
185
|
+
this.cancelFieldValidation(t);
|
|
186
|
+
for (const s of t.elements)
|
|
187
|
+
s.removeEventListener("blur", this.handleGroupEvent(e)), s.removeEventListener("input", this.handleGroupEvent(e)), s.removeEventListener("change", this.handleGroupEvent(e));
|
|
188
|
+
}
|
|
189
|
+
return this.groups.delete(e), this;
|
|
190
|
+
}
|
|
191
|
+
async validate(e = !1) {
|
|
192
|
+
if (this.destroyed) return !1;
|
|
193
|
+
for (const [, i] of this.fields)
|
|
194
|
+
!e && i.validated || await this.validateFieldState(i);
|
|
195
|
+
for (const [, i] of this.groups)
|
|
196
|
+
!e && i.validated || await this.validateGroupState(i);
|
|
197
|
+
const t = this.getAllErrors(), s = t.length === 0;
|
|
198
|
+
if (this.fireHooks(s, t), !s && this.config.focusOnError) {
|
|
199
|
+
const i = [];
|
|
200
|
+
for (const [, n] of this.fields)
|
|
201
|
+
n.errors.length > 0 && i.push({ element: n.field, errors: n.errors });
|
|
202
|
+
g(i);
|
|
203
|
+
}
|
|
204
|
+
return s;
|
|
205
|
+
}
|
|
206
|
+
async validateField(e) {
|
|
207
|
+
const t = this.fields.get(e);
|
|
208
|
+
return t ? (await this.validateFieldState(t), t.errors.length === 0) : !0;
|
|
209
|
+
}
|
|
210
|
+
async validateGroup(e) {
|
|
211
|
+
const t = this.groups.get(e);
|
|
212
|
+
return t ? (await this.validateGroupState(t), t.errors.length === 0) : !0;
|
|
213
|
+
}
|
|
214
|
+
async revalidate() {
|
|
215
|
+
for (const [, e] of this.fields)
|
|
216
|
+
e.validated = !1;
|
|
217
|
+
for (const [, e] of this.groups)
|
|
218
|
+
e.validated = !1;
|
|
219
|
+
return this.validate(!0);
|
|
220
|
+
}
|
|
221
|
+
async revalidateField(e) {
|
|
222
|
+
const t = this.fields.get(e);
|
|
223
|
+
return t ? (t.validated = !1, await this.validateFieldState(t), t.errors.length === 0) : !0;
|
|
224
|
+
}
|
|
225
|
+
async revalidateGroup(e) {
|
|
226
|
+
const t = this.groups.get(e);
|
|
227
|
+
return t ? (t.validated = !1, await this.validateGroupState(t), t.errors.length === 0) : !0;
|
|
228
|
+
}
|
|
229
|
+
clearErrors() {
|
|
230
|
+
for (const [, e] of this.fields)
|
|
231
|
+
this.cancelFieldValidation(e), e.errors = [], e.validated = !1, e.pending = !1, h(e.field, e.config, this.config);
|
|
232
|
+
for (const [, e] of this.groups)
|
|
233
|
+
this.cancelFieldValidation(e), e.errors = [], e.validated = !1, e.pending = !1;
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
refresh() {
|
|
237
|
+
const e = [...this.fields.entries()];
|
|
238
|
+
for (const [s, i] of e) {
|
|
239
|
+
const n = m(s, this.form);
|
|
240
|
+
n ? i.field = n : this.fields.delete(s);
|
|
241
|
+
}
|
|
242
|
+
const t = [...this.groups.entries()];
|
|
243
|
+
for (const [s, i] of t) {
|
|
244
|
+
const n = p(s, this.form);
|
|
245
|
+
n.length > 0 ? i.elements = n : this.groups.delete(s);
|
|
246
|
+
}
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
destroy() {
|
|
250
|
+
this.unbindEvents();
|
|
251
|
+
for (const [e, t] of this.fields)
|
|
252
|
+
this.cancelFieldValidation(t), h(t.field, t.config, this.config), t.field.removeEventListener("blur", this.handleBlur(e)), t.field.removeEventListener("input", this.handleChange(e)), t.field.removeEventListener("change", this.handleChange(e));
|
|
253
|
+
for (const [e, t] of this.groups) {
|
|
254
|
+
this.cancelFieldValidation(t);
|
|
255
|
+
for (const s of t.elements)
|
|
256
|
+
s.removeEventListener("blur", this.handleGroupEvent(e)), s.removeEventListener("input", this.handleGroupEvent(e)), s.removeEventListener("change", this.handleGroupEvent(e));
|
|
257
|
+
}
|
|
258
|
+
this.fields.clear(), this.groups.clear(), this.successCallbacks.length = 0, this.failCallbacks.length = 0, this.validateCallbacks.length = 0, this.destroyed = !0;
|
|
259
|
+
}
|
|
260
|
+
setLocale(e) {
|
|
261
|
+
return this.locale = { ...this.locale, ...e }, this;
|
|
262
|
+
}
|
|
263
|
+
onSuccess(e) {
|
|
264
|
+
return this.successCallbacks.push(e), this;
|
|
265
|
+
}
|
|
266
|
+
onFail(e) {
|
|
267
|
+
return this.failCallbacks.push(e), this;
|
|
268
|
+
}
|
|
269
|
+
onValidate(e) {
|
|
270
|
+
return this.validateCallbacks.push(e), this;
|
|
271
|
+
}
|
|
272
|
+
getErrors() {
|
|
273
|
+
return this.getAllErrors();
|
|
274
|
+
}
|
|
275
|
+
getFieldErrors(e) {
|
|
276
|
+
var t;
|
|
277
|
+
return ((t = this.fields.get(e)) == null ? void 0 : t.errors) ?? [];
|
|
278
|
+
}
|
|
279
|
+
getGroupErrors(e) {
|
|
280
|
+
var t;
|
|
281
|
+
return ((t = this.groups.get(e)) == null ? void 0 : t.errors) ?? [];
|
|
282
|
+
}
|
|
283
|
+
isFieldPending(e) {
|
|
284
|
+
var t;
|
|
285
|
+
return ((t = this.fields.get(e)) == null ? void 0 : t.pending) ?? !1;
|
|
286
|
+
}
|
|
287
|
+
isPending() {
|
|
288
|
+
for (const [, e] of this.fields)
|
|
289
|
+
if (e.pending) return !0;
|
|
290
|
+
for (const [, e] of this.groups)
|
|
291
|
+
if (e.pending) return !0;
|
|
292
|
+
return !1;
|
|
293
|
+
}
|
|
294
|
+
async validateFieldState(e) {
|
|
295
|
+
this.cancelFieldValidation(e);
|
|
296
|
+
const t = new AbortController();
|
|
297
|
+
e.abortController = t, e.pending = !0, e.errors = [], f(e.field, e.config, this.config);
|
|
298
|
+
const s = F(e.field), i = M(e.field, e.config);
|
|
299
|
+
try {
|
|
300
|
+
for (const n of e.rules) {
|
|
301
|
+
if (t.signal.aborted) break;
|
|
302
|
+
try {
|
|
303
|
+
const a = n.validate(s, this.form, e.config, t.signal), o = a instanceof Promise ? await a : a;
|
|
304
|
+
if (t.signal.aborted) break;
|
|
305
|
+
if (o === !0 || o === void 0)
|
|
306
|
+
continue;
|
|
307
|
+
let c;
|
|
308
|
+
if (typeof o == "string" ? c = o : typeof o == "object" && o !== null && "message" in o ? c = String(o.message) : c = this.getMessage(n.name, e.config, i), e.errors.push(
|
|
309
|
+
l(n.name, c, e.field.name || i, n.name, s)
|
|
310
|
+
), !this.config.collectAll) break;
|
|
311
|
+
} catch (a) {
|
|
312
|
+
if (t.signal.aborted || (this.config.debug && console.error(`FormGuard: rule "${n.name}" threw:`, a), e.errors.push(
|
|
313
|
+
l(
|
|
314
|
+
n.name,
|
|
315
|
+
this.getMessage(n.name, e.config, i),
|
|
316
|
+
e.field.name || i,
|
|
317
|
+
n.name,
|
|
318
|
+
s
|
|
319
|
+
)
|
|
320
|
+
), !this.config.collectAll)) break;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
} finally {
|
|
324
|
+
e.abortController === t && (e.pending = !1, e.validated = !0, e.abortController = null, t.signal.aborted || (e.errors.length > 0 ? E(e.field, e.errors[0], e.config, this.config) : L(e.field, e.config, this.config)));
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
async validateGroupState(e) {
|
|
328
|
+
this.cancelFieldValidation(e);
|
|
329
|
+
const t = new AbortController();
|
|
330
|
+
e.abortController = t, e.pending = !0, e.errors = [];
|
|
331
|
+
const s = k(e.elements), i = e.config.label ?? e.config.name ?? "group";
|
|
332
|
+
try {
|
|
333
|
+
for (const n of e.rules) {
|
|
334
|
+
if (t.signal.aborted) break;
|
|
335
|
+
try {
|
|
336
|
+
const a = n.validate(s, this.form, e.config, t.signal), o = a instanceof Promise ? await a : a;
|
|
337
|
+
if (t.signal.aborted) break;
|
|
338
|
+
if (o === !0 || o === void 0)
|
|
339
|
+
continue;
|
|
340
|
+
let c;
|
|
341
|
+
if (typeof o == "string" ? c = o : c = this.getMessage(n.name, e.config, i), e.errors.push(
|
|
342
|
+
l(n.name, c, i, n.name, s)
|
|
343
|
+
), !this.config.collectAll) break;
|
|
344
|
+
} catch (a) {
|
|
345
|
+
if (t.signal.aborted || (this.config.debug && console.error(`FormGuard: rule "${n.name}" threw:`, a), e.errors.push(
|
|
346
|
+
l(n.name, this.getMessage(n.name, e.config, i), i, n.name, s)
|
|
347
|
+
), !this.config.collectAll)) break;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
} finally {
|
|
351
|
+
e.abortController === t && (e.pending = !1, e.validated = !0, e.abortController = null);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
cancelFieldValidation(e) {
|
|
355
|
+
e.debounceTimer !== null && (clearTimeout(e.debounceTimer), e.debounceTimer = null), e.abortController && (e.abortController.abort(), e.abortController = null, e.pending = !1);
|
|
356
|
+
}
|
|
357
|
+
getMessage(e, t, s) {
|
|
358
|
+
var n;
|
|
359
|
+
const i = (n = t == null ? void 0 : t.messages) == null ? void 0 : n[e];
|
|
360
|
+
return i || (this.locale[e] ?? e);
|
|
361
|
+
}
|
|
362
|
+
getAllErrors() {
|
|
363
|
+
const e = [];
|
|
364
|
+
for (const [, t] of this.fields)
|
|
365
|
+
e.push(...t.errors);
|
|
366
|
+
for (const [, t] of this.groups)
|
|
367
|
+
e.push(...t.errors);
|
|
368
|
+
return e;
|
|
369
|
+
}
|
|
370
|
+
fireHooks(e, t) {
|
|
371
|
+
const s = {};
|
|
372
|
+
for (const [a, o] of this.fields)
|
|
373
|
+
o.errors.length > 0 && (s[a] = [...o.errors]);
|
|
374
|
+
const i = {};
|
|
375
|
+
for (const [a, o] of this.groups)
|
|
376
|
+
o.errors.length > 0 && (i[a] = [...o.errors]);
|
|
377
|
+
const n = { valid: e, errors: t, fields: s, groups: i };
|
|
378
|
+
for (const a of this.validateCallbacks)
|
|
379
|
+
a(n);
|
|
380
|
+
if (e)
|
|
381
|
+
for (const a of this.successCallbacks)
|
|
382
|
+
a();
|
|
383
|
+
else
|
|
384
|
+
for (const a of this.failCallbacks)
|
|
385
|
+
a(t);
|
|
386
|
+
}
|
|
387
|
+
handleBlur(e) {
|
|
388
|
+
return () => {
|
|
389
|
+
const t = this.fields.get(e);
|
|
390
|
+
t && this.debounceValidate(() => this.validateFieldState(t), t);
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
handleChange(e) {
|
|
394
|
+
return () => {
|
|
395
|
+
const t = this.fields.get(e);
|
|
396
|
+
t && (t.validated = !1, this.debounceValidate(() => this.validateFieldState(t), t));
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
handleGroupEvent(e) {
|
|
400
|
+
return () => {
|
|
401
|
+
const t = this.groups.get(e);
|
|
402
|
+
t && (t.validated = !1, this.debounceValidate(() => this.validateGroupState(t), t));
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
debounceValidate(e, t) {
|
|
406
|
+
const s = "config" in t && "debounceMs" in t.config ? t.config.debounceMs ?? this.config.debounceMs ?? 0 : this.config.debounceMs ?? 0;
|
|
407
|
+
s && s > 0 ? (t.debounceTimer !== null && clearTimeout(t.debounceTimer), t.pending = !0, t instanceof Object && "field" in t && f(t.field, t.config, this.config), t.debounceTimer = setTimeout(() => {
|
|
408
|
+
t.debounceTimer = null, e();
|
|
409
|
+
}, s)) : e();
|
|
410
|
+
}
|
|
411
|
+
bindEvents() {
|
|
412
|
+
this.config.mode === "onSubmit" && (this.boundSubmit = this.handleSubmit.bind(this), this.form.addEventListener("submit", this.boundSubmit), this.form.setAttribute("novalidate", ""));
|
|
413
|
+
}
|
|
414
|
+
unbindEvents() {
|
|
415
|
+
this.boundSubmit && (this.form.removeEventListener("submit", this.boundSubmit), this.boundSubmit = null), this.boundBlur && (this.form.removeEventListener("blur", this.boundBlur), this.boundBlur = null), this.boundChange && (this.form.removeEventListener("input", this.boundChange), this.boundChange = null), this.form.removeAttribute("novalidate");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const T = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/;
|
|
419
|
+
function w(r) {
|
|
420
|
+
return {
|
|
421
|
+
name: "required",
|
|
422
|
+
validate(e) {
|
|
423
|
+
return typeof e == "boolean" ? e === !0 || r || !1 : typeof e == "string" ? e.trim().length > 0 || r || !1 : e instanceof FileList || Array.isArray(e) ? e.length > 0 || r || !1 : e != null && e !== "" || r || !1;
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function H(r) {
|
|
428
|
+
return {
|
|
429
|
+
name: "email",
|
|
430
|
+
validate(e) {
|
|
431
|
+
return typeof e != "string" || e.trim() === "" ? !0 : T.test(e.trim()) || r || !1;
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function x(r, e) {
|
|
436
|
+
return {
|
|
437
|
+
name: "minLength",
|
|
438
|
+
validate(t) {
|
|
439
|
+
const s = typeof t == "string" ? t.trim().length : String(t ?? "").length;
|
|
440
|
+
return s === 0 ? !0 : s >= r || e || !1;
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
function V(r, e) {
|
|
445
|
+
return {
|
|
446
|
+
name: "maxLength",
|
|
447
|
+
validate(t) {
|
|
448
|
+
return (typeof t == "string" ? t.trim().length : String(t ?? "").length) <= r || e || !1;
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
function P(r) {
|
|
453
|
+
return {
|
|
454
|
+
name: "number",
|
|
455
|
+
validate(e) {
|
|
456
|
+
return typeof e != "string" || e.trim() === "" ? !0 : !isNaN(Number(e)) || r || !1;
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
function I(r) {
|
|
461
|
+
return {
|
|
462
|
+
name: "integer",
|
|
463
|
+
validate(e) {
|
|
464
|
+
if (typeof e != "string" || e.trim() === "") return !0;
|
|
465
|
+
const t = Number(e);
|
|
466
|
+
return !isNaN(t) && Number.isInteger(t) || r || !1;
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
function $(r, e) {
|
|
471
|
+
return {
|
|
472
|
+
name: "min",
|
|
473
|
+
validate(t) {
|
|
474
|
+
if (typeof t != "string" || t.trim() === "") return !0;
|
|
475
|
+
const s = Number(t);
|
|
476
|
+
return isNaN(s) ? !0 : s >= r || e || !1;
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
function q(r, e) {
|
|
481
|
+
return {
|
|
482
|
+
name: "max",
|
|
483
|
+
validate(t) {
|
|
484
|
+
if (typeof t != "string" || t.trim() === "") return !0;
|
|
485
|
+
const s = Number(t);
|
|
486
|
+
return isNaN(s) ? !0 : s <= r || e || !1;
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
function N(r, e) {
|
|
491
|
+
return {
|
|
492
|
+
name: "pattern",
|
|
493
|
+
validate(t) {
|
|
494
|
+
return typeof t != "string" || t.trim() === "" ? !0 : r.test(t) || e || !1;
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function B(r, e) {
|
|
499
|
+
return {
|
|
500
|
+
name: "sameAs",
|
|
501
|
+
validate(t, s) {
|
|
502
|
+
const i = s.querySelector(
|
|
503
|
+
`[name="${r}"]`
|
|
504
|
+
);
|
|
505
|
+
if (!i) return !0;
|
|
506
|
+
const n = typeof t == "string" ? t : "", a = i instanceof HTMLInputElement && (i.type === "checkbox" || i.type === "radio") ? i.checked : i.value;
|
|
507
|
+
return n === String(a) || e || !1;
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
function _(r) {
|
|
512
|
+
return {
|
|
513
|
+
name: "files",
|
|
514
|
+
validate(e) {
|
|
515
|
+
return e instanceof FileList ? e.length > 0 || r || !1 : !0;
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
function z(r, e) {
|
|
520
|
+
return {
|
|
521
|
+
name: "minFiles",
|
|
522
|
+
validate(t) {
|
|
523
|
+
return !(t instanceof FileList) || t.length === 0 ? !0 : t.length >= r || e || !1;
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function Z(r, e) {
|
|
528
|
+
return {
|
|
529
|
+
name: "maxFiles",
|
|
530
|
+
validate(t) {
|
|
531
|
+
return t instanceof FileList ? t.length <= r || e || !1 : !0;
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
function O(r, e) {
|
|
536
|
+
return {
|
|
537
|
+
name: e ?? "custom",
|
|
538
|
+
validate(t, s, i, n) {
|
|
539
|
+
return r(t, s, n);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function W(r, e) {
|
|
544
|
+
return {
|
|
545
|
+
name: (e == null ? void 0 : e.name) ?? "async",
|
|
546
|
+
validate(t, s, i, n) {
|
|
547
|
+
return r(t, s, n);
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
function D(r) {
|
|
552
|
+
const e = /[A-Z]/, t = /[a-z]/, s = /\d/, i = /[!@#$%^&*(),.?":{}|<>_\-+=\[\]\\;'/`~]/;
|
|
553
|
+
return {
|
|
554
|
+
name: "strongPassword",
|
|
555
|
+
validate(n) {
|
|
556
|
+
return typeof n != "string" || n.trim() === "" ? !0 : e.test(n) && t.test(n) && s.test(n) && i.test(n) && n.length >= 8 || r || !1;
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
export {
|
|
561
|
+
S as FormGuard,
|
|
562
|
+
W as asyncRule,
|
|
563
|
+
O as custom,
|
|
564
|
+
H as email,
|
|
565
|
+
y as en,
|
|
566
|
+
_ as files,
|
|
567
|
+
I as integer,
|
|
568
|
+
q as max,
|
|
569
|
+
Z as maxFiles,
|
|
570
|
+
V as maxLength,
|
|
571
|
+
$ as min,
|
|
572
|
+
z as minFiles,
|
|
573
|
+
x as minLength,
|
|
574
|
+
P as number,
|
|
575
|
+
N as pattern,
|
|
576
|
+
w as required,
|
|
577
|
+
B as sameAs,
|
|
578
|
+
D as strongPassword
|
|
579
|
+
};
|
|
580
|
+
//# sourceMappingURL=form-guard.js.map
|