dino-validation 0.4.0 → 1.0.0
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 +47 -18
- package/THIRD_PARTY_NOTICES.md +69 -0
- package/dist/dv.esm.js +704 -101
- package/dist/dv.esm.js.map +1 -1
- package/dist/dv.js +704 -100
- package/dist/dv.js.map +1 -1
- package/dist/dv.min.js +2 -2
- package/dist/dv.min.js.map +1 -1
- package/dist/dv.unobtrusive.esm.js +428 -0
- package/dist/dv.unobtrusive.esm.js.map +1 -0
- package/dist/dv.unobtrusive.js +440 -0
- package/dist/dv.unobtrusive.js.map +1 -0
- package/dist/dv.unobtrusive.min.js +15 -0
- package/dist/dv.unobtrusive.min.js.map +1 -0
- package/dist/localization/messages_ar.esm.js +2 -2
- package/dist/localization/messages_ar.js +3 -3
- package/dist/localization/messages_ar.js.map +1 -1
- package/dist/localization/messages_ar.min.js +2 -2
- package/dist/localization/messages_ar.min.js.map +1 -1
- package/dist/localization/messages_az.esm.js +2 -2
- package/dist/localization/messages_az.js +3 -3
- package/dist/localization/messages_az.js.map +1 -1
- package/dist/localization/messages_az.min.js +2 -2
- package/dist/localization/messages_az.min.js.map +1 -1
- package/dist/localization/messages_bg.esm.js +2 -2
- package/dist/localization/messages_bg.js +3 -3
- package/dist/localization/messages_bg.js.map +1 -1
- package/dist/localization/messages_bg.min.js +2 -2
- package/dist/localization/messages_bg.min.js.map +1 -1
- package/dist/localization/messages_bn_BD.esm.js +2 -2
- package/dist/localization/messages_bn_BD.js +3 -3
- package/dist/localization/messages_bn_BD.js.map +1 -1
- package/dist/localization/messages_bn_BD.min.js +2 -2
- package/dist/localization/messages_bn_BD.min.js.map +1 -1
- package/dist/localization/messages_ca.esm.js +2 -2
- package/dist/localization/messages_ca.js +3 -3
- package/dist/localization/messages_ca.js.map +1 -1
- package/dist/localization/messages_ca.min.js +2 -2
- package/dist/localization/messages_ca.min.js.map +1 -1
- package/dist/localization/messages_cs.esm.js +2 -2
- package/dist/localization/messages_cs.js +3 -3
- package/dist/localization/messages_cs.js.map +1 -1
- package/dist/localization/messages_cs.min.js +2 -2
- package/dist/localization/messages_cs.min.js.map +1 -1
- package/dist/localization/messages_da.esm.js +2 -2
- package/dist/localization/messages_da.js +3 -3
- package/dist/localization/messages_da.js.map +1 -1
- package/dist/localization/messages_da.min.js +2 -2
- package/dist/localization/messages_da.min.js.map +1 -1
- package/dist/localization/messages_de.esm.js +2 -2
- package/dist/localization/messages_de.js +3 -3
- package/dist/localization/messages_de.js.map +1 -1
- package/dist/localization/messages_de.min.js +2 -2
- package/dist/localization/messages_de.min.js.map +1 -1
- package/dist/localization/messages_el.esm.js +2 -2
- package/dist/localization/messages_el.js +3 -3
- package/dist/localization/messages_el.js.map +1 -1
- package/dist/localization/messages_el.min.js +2 -2
- package/dist/localization/messages_el.min.js.map +1 -1
- package/dist/localization/messages_es.esm.js +2 -2
- package/dist/localization/messages_es.js +3 -3
- package/dist/localization/messages_es.js.map +1 -1
- package/dist/localization/messages_es.min.js +2 -2
- package/dist/localization/messages_es.min.js.map +1 -1
- package/dist/localization/messages_es_AR.esm.js +2 -2
- package/dist/localization/messages_es_AR.js +3 -3
- package/dist/localization/messages_es_AR.js.map +1 -1
- package/dist/localization/messages_es_AR.min.js +2 -2
- package/dist/localization/messages_es_AR.min.js.map +1 -1
- package/dist/localization/messages_es_PE.esm.js +2 -2
- package/dist/localization/messages_es_PE.js +3 -3
- package/dist/localization/messages_es_PE.js.map +1 -1
- package/dist/localization/messages_es_PE.min.js +2 -2
- package/dist/localization/messages_es_PE.min.js.map +1 -1
- package/dist/localization/messages_et.esm.js +2 -2
- package/dist/localization/messages_et.js +3 -3
- package/dist/localization/messages_et.js.map +1 -1
- package/dist/localization/messages_et.min.js +2 -2
- package/dist/localization/messages_et.min.js.map +1 -1
- package/dist/localization/messages_eu.esm.js +2 -2
- package/dist/localization/messages_eu.js +3 -3
- package/dist/localization/messages_eu.js.map +1 -1
- package/dist/localization/messages_eu.min.js +2 -2
- package/dist/localization/messages_eu.min.js.map +1 -1
- package/dist/localization/messages_fa.esm.js +2 -2
- package/dist/localization/messages_fa.js +3 -3
- package/dist/localization/messages_fa.js.map +1 -1
- package/dist/localization/messages_fa.min.js +2 -2
- package/dist/localization/messages_fa.min.js.map +1 -1
- package/dist/localization/messages_fi.esm.js +2 -2
- package/dist/localization/messages_fi.js +3 -3
- package/dist/localization/messages_fi.js.map +1 -1
- package/dist/localization/messages_fi.min.js +2 -2
- package/dist/localization/messages_fi.min.js.map +1 -1
- package/dist/localization/messages_fr.esm.js +2 -2
- package/dist/localization/messages_fr.js +3 -3
- package/dist/localization/messages_fr.js.map +1 -1
- package/dist/localization/messages_fr.min.js +2 -2
- package/dist/localization/messages_fr.min.js.map +1 -1
- package/dist/localization/messages_ge.esm.js +2 -2
- package/dist/localization/messages_ge.js +3 -3
- package/dist/localization/messages_ge.js.map +1 -1
- package/dist/localization/messages_ge.min.js +2 -2
- package/dist/localization/messages_ge.min.js.map +1 -1
- package/dist/localization/messages_gl.esm.js +2 -2
- package/dist/localization/messages_gl.js +3 -3
- package/dist/localization/messages_gl.js.map +1 -1
- package/dist/localization/messages_gl.min.js +2 -2
- package/dist/localization/messages_gl.min.js.map +1 -1
- package/dist/localization/messages_he.esm.js +2 -2
- package/dist/localization/messages_he.js +3 -3
- package/dist/localization/messages_he.js.map +1 -1
- package/dist/localization/messages_he.min.js +2 -2
- package/dist/localization/messages_he.min.js.map +1 -1
- package/dist/localization/messages_hi.esm.js +2 -2
- package/dist/localization/messages_hi.js +3 -3
- package/dist/localization/messages_hi.js.map +1 -1
- package/dist/localization/messages_hi.min.js +2 -2
- package/dist/localization/messages_hi.min.js.map +1 -1
- package/dist/localization/messages_hr.esm.js +2 -2
- package/dist/localization/messages_hr.js +3 -3
- package/dist/localization/messages_hr.js.map +1 -1
- package/dist/localization/messages_hr.min.js +2 -2
- package/dist/localization/messages_hr.min.js.map +1 -1
- package/dist/localization/messages_hu.esm.js +2 -2
- package/dist/localization/messages_hu.js +3 -3
- package/dist/localization/messages_hu.js.map +1 -1
- package/dist/localization/messages_hu.min.js +2 -2
- package/dist/localization/messages_hu.min.js.map +1 -1
- package/dist/localization/messages_hy_AM.esm.js +2 -2
- package/dist/localization/messages_hy_AM.js +3 -3
- package/dist/localization/messages_hy_AM.js.map +1 -1
- package/dist/localization/messages_hy_AM.min.js +2 -2
- package/dist/localization/messages_hy_AM.min.js.map +1 -1
- package/dist/localization/messages_id.esm.js +2 -2
- package/dist/localization/messages_id.js +3 -3
- package/dist/localization/messages_id.js.map +1 -1
- package/dist/localization/messages_id.min.js +2 -2
- package/dist/localization/messages_id.min.js.map +1 -1
- package/dist/localization/messages_is.esm.js +2 -2
- package/dist/localization/messages_is.js +3 -3
- package/dist/localization/messages_is.js.map +1 -1
- package/dist/localization/messages_is.min.js +2 -2
- package/dist/localization/messages_is.min.js.map +1 -1
- package/dist/localization/messages_it.esm.js +2 -2
- package/dist/localization/messages_it.js +3 -3
- package/dist/localization/messages_it.js.map +1 -1
- package/dist/localization/messages_it.min.js +2 -2
- package/dist/localization/messages_it.min.js.map +1 -1
- package/dist/localization/messages_ja.esm.js +2 -2
- package/dist/localization/messages_ja.js +3 -3
- package/dist/localization/messages_ja.js.map +1 -1
- package/dist/localization/messages_ja.min.js +2 -2
- package/dist/localization/messages_ja.min.js.map +1 -1
- package/dist/localization/messages_ka.esm.js +2 -2
- package/dist/localization/messages_ka.js +3 -3
- package/dist/localization/messages_ka.js.map +1 -1
- package/dist/localization/messages_ka.min.js +2 -2
- package/dist/localization/messages_ka.min.js.map +1 -1
- package/dist/localization/messages_kk.esm.js +2 -2
- package/dist/localization/messages_kk.js +3 -3
- package/dist/localization/messages_kk.js.map +1 -1
- package/dist/localization/messages_kk.min.js +2 -2
- package/dist/localization/messages_kk.min.js.map +1 -1
- package/dist/localization/messages_ko.esm.js +2 -2
- package/dist/localization/messages_ko.js +3 -3
- package/dist/localization/messages_ko.js.map +1 -1
- package/dist/localization/messages_ko.min.js +2 -2
- package/dist/localization/messages_ko.min.js.map +1 -1
- package/dist/localization/messages_lt.esm.js +2 -2
- package/dist/localization/messages_lt.js +3 -3
- package/dist/localization/messages_lt.js.map +1 -1
- package/dist/localization/messages_lt.min.js +2 -2
- package/dist/localization/messages_lt.min.js.map +1 -1
- package/dist/localization/messages_lv.esm.js +2 -2
- package/dist/localization/messages_lv.js +3 -3
- package/dist/localization/messages_lv.js.map +1 -1
- package/dist/localization/messages_lv.min.js +2 -2
- package/dist/localization/messages_lv.min.js.map +1 -1
- package/dist/localization/messages_mk.esm.js +2 -2
- package/dist/localization/messages_mk.js +3 -3
- package/dist/localization/messages_mk.js.map +1 -1
- package/dist/localization/messages_mk.min.js +2 -2
- package/dist/localization/messages_mk.min.js.map +1 -1
- package/dist/localization/messages_my.esm.js +2 -2
- package/dist/localization/messages_my.js +3 -3
- package/dist/localization/messages_my.js.map +1 -1
- package/dist/localization/messages_my.min.js +2 -2
- package/dist/localization/messages_my.min.js.map +1 -1
- package/dist/localization/messages_nl.esm.js +2 -2
- package/dist/localization/messages_nl.js +3 -3
- package/dist/localization/messages_nl.js.map +1 -1
- package/dist/localization/messages_nl.min.js +2 -2
- package/dist/localization/messages_nl.min.js.map +1 -1
- package/dist/localization/messages_no.esm.js +2 -2
- package/dist/localization/messages_no.js +3 -3
- package/dist/localization/messages_no.js.map +1 -1
- package/dist/localization/messages_no.min.js +2 -2
- package/dist/localization/messages_no.min.js.map +1 -1
- package/dist/localization/messages_pl.esm.js +2 -2
- package/dist/localization/messages_pl.js +3 -3
- package/dist/localization/messages_pl.js.map +1 -1
- package/dist/localization/messages_pl.min.js +2 -2
- package/dist/localization/messages_pl.min.js.map +1 -1
- package/dist/localization/messages_pt_BR.esm.js +2 -2
- package/dist/localization/messages_pt_BR.js +3 -3
- package/dist/localization/messages_pt_BR.js.map +1 -1
- package/dist/localization/messages_pt_BR.min.js +2 -2
- package/dist/localization/messages_pt_BR.min.js.map +1 -1
- package/dist/localization/messages_pt_PT.esm.js +2 -2
- package/dist/localization/messages_pt_PT.js +3 -3
- package/dist/localization/messages_pt_PT.js.map +1 -1
- package/dist/localization/messages_pt_PT.min.js +2 -2
- package/dist/localization/messages_pt_PT.min.js.map +1 -1
- package/dist/localization/messages_ro.esm.js +2 -2
- package/dist/localization/messages_ro.js +3 -3
- package/dist/localization/messages_ro.js.map +1 -1
- package/dist/localization/messages_ro.min.js +2 -2
- package/dist/localization/messages_ro.min.js.map +1 -1
- package/dist/localization/messages_ru.esm.js +2 -2
- package/dist/localization/messages_ru.js +3 -3
- package/dist/localization/messages_ru.js.map +1 -1
- package/dist/localization/messages_ru.min.js +2 -2
- package/dist/localization/messages_ru.min.js.map +1 -1
- package/dist/localization/messages_sd.esm.js +2 -2
- package/dist/localization/messages_sd.js +3 -3
- package/dist/localization/messages_sd.js.map +1 -1
- package/dist/localization/messages_sd.min.js +2 -2
- package/dist/localization/messages_sd.min.js.map +1 -1
- package/dist/localization/messages_si.esm.js +2 -2
- package/dist/localization/messages_si.js +3 -3
- package/dist/localization/messages_si.js.map +1 -1
- package/dist/localization/messages_si.min.js +2 -2
- package/dist/localization/messages_si.min.js.map +1 -1
- package/dist/localization/messages_sk.esm.js +2 -2
- package/dist/localization/messages_sk.js +3 -3
- package/dist/localization/messages_sk.js.map +1 -1
- package/dist/localization/messages_sk.min.js +2 -2
- package/dist/localization/messages_sk.min.js.map +1 -1
- package/dist/localization/messages_sl.esm.js +2 -2
- package/dist/localization/messages_sl.js +3 -3
- package/dist/localization/messages_sl.js.map +1 -1
- package/dist/localization/messages_sl.min.js +2 -2
- package/dist/localization/messages_sl.min.js.map +1 -1
- package/dist/localization/messages_sr.esm.js +2 -2
- package/dist/localization/messages_sr.js +3 -3
- package/dist/localization/messages_sr.js.map +1 -1
- package/dist/localization/messages_sr.min.js +2 -2
- package/dist/localization/messages_sr.min.js.map +1 -1
- package/dist/localization/messages_sr_lat.esm.js +2 -2
- package/dist/localization/messages_sr_lat.js +3 -3
- package/dist/localization/messages_sr_lat.js.map +1 -1
- package/dist/localization/messages_sr_lat.min.js +2 -2
- package/dist/localization/messages_sr_lat.min.js.map +1 -1
- package/dist/localization/messages_sv.esm.js +2 -2
- package/dist/localization/messages_sv.js +3 -3
- package/dist/localization/messages_sv.js.map +1 -1
- package/dist/localization/messages_sv.min.js +2 -2
- package/dist/localization/messages_sv.min.js.map +1 -1
- package/dist/localization/messages_th.esm.js +2 -2
- package/dist/localization/messages_th.js +3 -3
- package/dist/localization/messages_th.js.map +1 -1
- package/dist/localization/messages_th.min.js +2 -2
- package/dist/localization/messages_th.min.js.map +1 -1
- package/dist/localization/messages_tj.esm.js +2 -2
- package/dist/localization/messages_tj.js +3 -3
- package/dist/localization/messages_tj.js.map +1 -1
- package/dist/localization/messages_tj.min.js +2 -2
- package/dist/localization/messages_tj.min.js.map +1 -1
- package/dist/localization/messages_tr.esm.js +2 -2
- package/dist/localization/messages_tr.js +3 -3
- package/dist/localization/messages_tr.js.map +1 -1
- package/dist/localization/messages_tr.min.js +2 -2
- package/dist/localization/messages_tr.min.js.map +1 -1
- package/dist/localization/messages_uk.esm.js +2 -2
- package/dist/localization/messages_uk.js +3 -3
- package/dist/localization/messages_uk.js.map +1 -1
- package/dist/localization/messages_uk.min.js +2 -2
- package/dist/localization/messages_uk.min.js.map +1 -1
- package/dist/localization/messages_ur.esm.js +2 -2
- package/dist/localization/messages_ur.js +3 -3
- package/dist/localization/messages_ur.js.map +1 -1
- package/dist/localization/messages_ur.min.js +2 -2
- package/dist/localization/messages_ur.min.js.map +1 -1
- package/dist/localization/messages_vi.esm.js +2 -2
- package/dist/localization/messages_vi.js +3 -3
- package/dist/localization/messages_vi.js.map +1 -1
- package/dist/localization/messages_vi.min.js +2 -2
- package/dist/localization/messages_vi.min.js.map +1 -1
- package/dist/localization/messages_zh.esm.js +2 -2
- package/dist/localization/messages_zh.js +3 -3
- package/dist/localization/messages_zh.js.map +1 -1
- package/dist/localization/messages_zh.min.js +2 -2
- package/dist/localization/messages_zh.min.js.map +1 -1
- package/dist/localization/messages_zh_TW.esm.js +2 -2
- package/dist/localization/messages_zh_TW.js +3 -3
- package/dist/localization/messages_zh_TW.js.map +1 -1
- package/dist/localization/messages_zh_TW.min.js +2 -2
- package/dist/localization/messages_zh_TW.min.js.map +1 -1
- package/dist/localization/methods_de.esm.js +2 -2
- package/dist/localization/methods_de.js +3 -3
- package/dist/localization/methods_de.js.map +1 -1
- package/dist/localization/methods_de.min.js +2 -2
- package/dist/localization/methods_de.min.js.map +1 -1
- package/dist/localization/methods_es_CL.esm.js +2 -2
- package/dist/localization/methods_es_CL.js +3 -3
- package/dist/localization/methods_es_CL.js.map +1 -1
- package/dist/localization/methods_es_CL.min.js +2 -2
- package/dist/localization/methods_es_CL.min.js.map +1 -1
- package/dist/localization/methods_fi.esm.js +2 -2
- package/dist/localization/methods_fi.js +3 -3
- package/dist/localization/methods_fi.js.map +1 -1
- package/dist/localization/methods_fi.min.js +2 -2
- package/dist/localization/methods_fi.min.js.map +1 -1
- package/dist/localization/methods_it.esm.js +2 -2
- package/dist/localization/methods_it.js +3 -3
- package/dist/localization/methods_it.js.map +1 -1
- package/dist/localization/methods_it.min.js +2 -2
- package/dist/localization/methods_it.min.js.map +1 -1
- package/dist/localization/methods_nl.esm.js +2 -2
- package/dist/localization/methods_nl.js +3 -3
- package/dist/localization/methods_nl.js.map +1 -1
- package/dist/localization/methods_nl.min.js +2 -2
- package/dist/localization/methods_nl.min.js.map +1 -1
- package/dist/localization/methods_pt.esm.js +2 -2
- package/dist/localization/methods_pt.js +3 -3
- package/dist/localization/methods_pt.js.map +1 -1
- package/dist/localization/methods_pt.min.js +2 -2
- package/dist/localization/methods_pt.min.js.map +1 -1
- package/dist/types/defaults.d.ts.map +1 -1
- package/dist/types/eventManager.d.ts +5 -2
- package/dist/types/eventManager.d.ts.map +1 -1
- package/dist/types/helpers.d.ts +19 -0
- package/dist/types/helpers.d.ts.map +1 -1
- package/dist/types/index.d.ts +20 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/messages.d.ts +2 -2
- package/dist/types/messages.d.ts.map +1 -1
- package/dist/types/methods.d.ts.map +1 -1
- package/dist/types/objectStore.d.ts +6 -0
- package/dist/types/objectStore.d.ts.map +1 -1
- package/dist/types/rules.d.ts.map +1 -1
- package/dist/types/types.d.ts +29 -4
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/unobtrusive.d.ts +104 -0
- package/dist/types/unobtrusive.d.ts.map +1 -0
- package/dist/types/validator.d.ts +62 -3
- package/dist/types/validator.d.ts.map +1 -1
- package/package.json +24 -7
package/dist/dv.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* dino-validation
|
|
2
|
+
* dino-validation v1.0.0
|
|
3
3
|
* https://github.com/cadamsmith/dino-validation
|
|
4
4
|
*
|
|
5
5
|
* Copyright (c) Jörn Zaefferer (jQuery Validation)
|
|
@@ -47,8 +47,10 @@
|
|
|
47
47
|
function findByName(form, name) {
|
|
48
48
|
const selector = `[name='${escapeCssMeta(name)}']`;
|
|
49
49
|
const elements = Array.from(form.querySelectorAll(selector));
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
// getAttribute: a field named "id" shadows form.id
|
|
51
|
+
const formId = form.getAttribute('id');
|
|
52
|
+
if (formId) {
|
|
53
|
+
const outsideElements = Array.from(document.querySelectorAll(selector)).filter((el) => el.matches(`[form="${escapeCssMeta(formId)}"]`));
|
|
52
54
|
elements.push(...outsideElements);
|
|
53
55
|
}
|
|
54
56
|
return elements;
|
|
@@ -146,7 +148,8 @@
|
|
|
146
148
|
params = [params];
|
|
147
149
|
}
|
|
148
150
|
params.forEach((param, index) => {
|
|
149
|
-
|
|
151
|
+
// a replacement function: $&, $' and $` in the parameter stay as they are
|
|
152
|
+
source = source.replace(new RegExp(`\\{${index}\\}`, 'g'), () => String(param));
|
|
150
153
|
});
|
|
151
154
|
return source;
|
|
152
155
|
}
|
|
@@ -221,11 +224,128 @@
|
|
|
221
224
|
element.style.display = 'none';
|
|
222
225
|
}
|
|
223
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Splits a selector list on its top-level commas, so `:not(a, b)` and
|
|
229
|
+
* `[title="a,b"]` stay whole.
|
|
230
|
+
* @param selector - selector list
|
|
231
|
+
* @return the selectors, trimmed, without empty ones
|
|
232
|
+
*/
|
|
233
|
+
function splitSelectorList(selector) {
|
|
234
|
+
const parts = [];
|
|
235
|
+
let depth = 0;
|
|
236
|
+
let quote = '';
|
|
237
|
+
let start = 0;
|
|
238
|
+
for (let i = 0; i < selector.length; i++) {
|
|
239
|
+
const c = selector[i];
|
|
240
|
+
if (quote) {
|
|
241
|
+
if (c === '\\')
|
|
242
|
+
i++;
|
|
243
|
+
else if (c === quote)
|
|
244
|
+
quote = '';
|
|
245
|
+
}
|
|
246
|
+
else if (c === '\\') {
|
|
247
|
+
i++;
|
|
248
|
+
}
|
|
249
|
+
else if (c === '"' || c === "'") {
|
|
250
|
+
quote = c;
|
|
251
|
+
}
|
|
252
|
+
else if (c === '(' || c === '[') {
|
|
253
|
+
depth++;
|
|
254
|
+
}
|
|
255
|
+
else if (c === ')' || c === ']') {
|
|
256
|
+
depth--;
|
|
257
|
+
}
|
|
258
|
+
else if (c === ',' && depth === 0) {
|
|
259
|
+
parts.push(selector.slice(start, i));
|
|
260
|
+
start = i + 1;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
parts.push(selector.slice(start));
|
|
264
|
+
return parts.map((part) => part.trim()).filter(Boolean);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Builds the test for the `ignore` setting: a selector list, or an array of
|
|
268
|
+
* them, where jQuery's `:hidden` means "not visible", also inside a compound
|
|
269
|
+
* selector such as `:hidden:not(.select2-hidden-accessible)`.
|
|
270
|
+
* An empty string or array ignores nothing. A selector the browser rejects
|
|
271
|
+
* ignores nothing (with a warning), so it can't turn validation off.
|
|
272
|
+
* @param ignore - the `ignore` setting
|
|
273
|
+
* @return function returning true for elements to ignore
|
|
274
|
+
*/
|
|
275
|
+
function ignoreMatcher(ignore) {
|
|
276
|
+
const selectors = (Array.isArray(ignore) ? ignore : [ignore || ''])
|
|
277
|
+
.flatMap((s) => splitSelectorList(String(s)))
|
|
278
|
+
.map((selector) => {
|
|
279
|
+
// :hidden alone, or as part of a compound selector: the rest of the
|
|
280
|
+
// selector must match too ("form :hidden" → "form *")
|
|
281
|
+
const hidden = /:hidden(?![\w-])/.test(selector);
|
|
282
|
+
const rest = selector
|
|
283
|
+
.replace(/(^|[\s>+~])?:hidden(?![\w-])/g, (_m, before) => before === undefined ? '' : `${before}*`)
|
|
284
|
+
.trim();
|
|
285
|
+
return { hidden, rest: rest === '*' ? '' : rest };
|
|
286
|
+
});
|
|
287
|
+
if (!selectors.length) {
|
|
288
|
+
return () => false;
|
|
289
|
+
}
|
|
290
|
+
const warned = new Set();
|
|
291
|
+
return (element) => selectors.some(({ hidden, rest }) => {
|
|
292
|
+
if (hidden && isVisible(element)) {
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
if (!rest) {
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
try {
|
|
299
|
+
return element.matches(rest);
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
if (!warned.has(rest)) {
|
|
303
|
+
warned.add(rest);
|
|
304
|
+
console.warn(`dv: ignore selector "${rest}" is not valid, skipped`);
|
|
305
|
+
}
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Whether a conditional parameter holds for the element, as jQuery's
|
|
312
|
+
* `depend`: a boolean, a selector (true if it matches in the element's
|
|
313
|
+
* form, e.g. '#other:checked') or a function of the element.
|
|
314
|
+
* @param param - the parameter
|
|
315
|
+
* @param element - the element being validated
|
|
316
|
+
* @return true if the rule applies
|
|
317
|
+
*/
|
|
318
|
+
function depend(param, element) {
|
|
319
|
+
if (typeof param === 'boolean') {
|
|
320
|
+
return param;
|
|
321
|
+
}
|
|
322
|
+
if (typeof param === 'function') {
|
|
323
|
+
return !!param.call(element, element);
|
|
324
|
+
}
|
|
325
|
+
if (typeof param === 'string') {
|
|
326
|
+
try {
|
|
327
|
+
return !!element.form?.querySelector(param);
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
// not a selector
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
224
336
|
|
|
225
337
|
class ObjectStore {
|
|
226
338
|
constructor(data) {
|
|
227
339
|
this._store = {};
|
|
228
340
|
this._store = data;
|
|
341
|
+
this._defaults = { ...data };
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Whether the entry is still the built-in one (not changed with
|
|
345
|
+
* `set`, `replace` or `dv.localize`).
|
|
346
|
+
*/
|
|
347
|
+
isDefault(key) {
|
|
348
|
+
return key in this._defaults && this._store[key] === this._defaults[key];
|
|
229
349
|
}
|
|
230
350
|
keys() {
|
|
231
351
|
return Object.keys(this._store);
|
|
@@ -265,13 +385,17 @@
|
|
|
265
385
|
regex,
|
|
266
386
|
nonalphamin: nonAlphaMin,
|
|
267
387
|
creditcard: creditCard,
|
|
388
|
+
extension,
|
|
389
|
+
remote,
|
|
268
390
|
});
|
|
269
391
|
/**
|
|
270
|
-
* Validates that the field has a value (is not blank).
|
|
271
|
-
*
|
|
392
|
+
* Validates that the field has a value (is not blank). The parameter can
|
|
393
|
+
* make it conditional: a boolean, a selector that must match in the form
|
|
394
|
+
* (e.g. '#other:checked'), or a function of the element.
|
|
395
|
+
* @return true if field is not blank, or the field isn't required now
|
|
272
396
|
*/
|
|
273
|
-
function required({ blank }) {
|
|
274
|
-
return !blank;
|
|
397
|
+
function required({ blank, param, element }) {
|
|
398
|
+
return !blank || !depend(param, element);
|
|
275
399
|
}
|
|
276
400
|
/**
|
|
277
401
|
* Validates that the field value has at least the minimum length.
|
|
@@ -387,8 +511,8 @@
|
|
|
387
511
|
* @return true if values match
|
|
388
512
|
*/
|
|
389
513
|
function equalTo({ value, param }) {
|
|
390
|
-
const target = document.querySelector(param);
|
|
391
|
-
return value === target.value;
|
|
514
|
+
const target = param instanceof Element ? param : document.querySelector(param);
|
|
515
|
+
return !!target && value === target.value;
|
|
392
516
|
}
|
|
393
517
|
/**
|
|
394
518
|
* Validates that the field value matches a regular expression.
|
|
@@ -422,6 +546,10 @@
|
|
|
422
546
|
if (blank) {
|
|
423
547
|
return true;
|
|
424
548
|
}
|
|
549
|
+
// only digits, spaces and dashes are allowed
|
|
550
|
+
if (/[^0-9 \-]+/.test(value)) {
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
425
553
|
const strippedValue = value.replace(/\D/g, '');
|
|
426
554
|
let nCheck = 0;
|
|
427
555
|
let bEven = false;
|
|
@@ -443,6 +571,77 @@
|
|
|
443
571
|
}
|
|
444
572
|
return nCheck % 10 === 0;
|
|
445
573
|
}
|
|
574
|
+
/**
|
|
575
|
+
* Validates that the field value (a file name) ends with one of the allowed extensions.
|
|
576
|
+
* Extensions are separated by commas or pipes, with or without a leading dot
|
|
577
|
+
* (e.g. "png,jpg", ".png,.jpg" or "png|jpe?g"). Defaults to common image types.
|
|
578
|
+
* For file inputs, every selected file is checked.
|
|
579
|
+
* @return true if blank or every file name has an allowed extension
|
|
580
|
+
*/
|
|
581
|
+
function extension({ blank, value, element, param, }) {
|
|
582
|
+
if (blank) {
|
|
583
|
+
return true;
|
|
584
|
+
}
|
|
585
|
+
const allowed = typeof param === 'string' && param.trim() !== ''
|
|
586
|
+
? param
|
|
587
|
+
.split(/[,|]/)
|
|
588
|
+
.map((ext) => ext.trim().replace(/^\./, ''))
|
|
589
|
+
.filter(Boolean)
|
|
590
|
+
.join('|')
|
|
591
|
+
: 'png|jpe?g|gif';
|
|
592
|
+
const re = new RegExp(`\\.(?:${allowed})$`, 'i');
|
|
593
|
+
const files = element.files;
|
|
594
|
+
if (files && files.length) {
|
|
595
|
+
return Array.from(files).every((file) => re.test(file.name));
|
|
596
|
+
}
|
|
597
|
+
return re.test(value);
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Asks the server whether the value is valid (e.g. "is this username free?").
|
|
601
|
+
* Sends the field as `name=value` (plus any `data`), GET by default, and
|
|
602
|
+
* expects JSON: true when valid; false, or an error message, when invalid.
|
|
603
|
+
* Results are cached by the validator until the value or data changes.
|
|
604
|
+
* @return true if blank, otherwise a promise of the server's answer
|
|
605
|
+
*/
|
|
606
|
+
function remote({ blank, value, element, param, signal, }) {
|
|
607
|
+
if (blank) {
|
|
608
|
+
return true;
|
|
609
|
+
}
|
|
610
|
+
const options = (typeof param === 'string' ? { url: param } : param);
|
|
611
|
+
const method = (options.type || options.method || 'GET').toUpperCase();
|
|
612
|
+
const fields = new URLSearchParams();
|
|
613
|
+
fields.append(element.name, value);
|
|
614
|
+
Object.entries(options.data ?? {}).forEach(([key, item]) => {
|
|
615
|
+
const fieldValue = typeof item === 'function' ? item() : item;
|
|
616
|
+
fields.set(key, fieldValue == null ? '' : String(fieldValue));
|
|
617
|
+
});
|
|
618
|
+
const url = new URL(options.url, document.baseURI);
|
|
619
|
+
const headers = { Accept: 'application/json' };
|
|
620
|
+
// like jQuery: marks the request as AJAX, but only on the same origin
|
|
621
|
+
// (on another origin it would need a CORS preflight)
|
|
622
|
+
if (url.origin === location.origin) {
|
|
623
|
+
headers['X-Requested-With'] = 'XMLHttpRequest';
|
|
624
|
+
}
|
|
625
|
+
const init = {
|
|
626
|
+
method,
|
|
627
|
+
headers: { ...headers, ...options.headers },
|
|
628
|
+
};
|
|
629
|
+
if (signal) {
|
|
630
|
+
init.signal = signal;
|
|
631
|
+
}
|
|
632
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
633
|
+
fields.forEach((fieldValue, key) => url.searchParams.set(key, fieldValue));
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
init.body = fields;
|
|
637
|
+
}
|
|
638
|
+
return fetch(url, init).then((response) => {
|
|
639
|
+
if (!response.ok) {
|
|
640
|
+
throw new Error(`remote: ${response.status} ${response.statusText}`);
|
|
641
|
+
}
|
|
642
|
+
return response.json();
|
|
643
|
+
});
|
|
644
|
+
}
|
|
446
645
|
|
|
447
646
|
/**
|
|
448
647
|
* Gets all validation rules for an element from multiple sources.
|
|
@@ -456,12 +655,23 @@
|
|
|
456
655
|
if (element == null || element.form == null) {
|
|
457
656
|
return {};
|
|
458
657
|
}
|
|
459
|
-
let data = normalizeRules(
|
|
658
|
+
let data = normalizeRules({
|
|
659
|
+
...classRules(element),
|
|
660
|
+
...attributeRules(element),
|
|
661
|
+
...dataRules(element),
|
|
662
|
+
...staticRules(element, settings),
|
|
663
|
+
}, element);
|
|
460
664
|
// Make sure required is at front
|
|
461
|
-
if (data
|
|
665
|
+
if ('required' in data) {
|
|
462
666
|
const param = data.required;
|
|
463
667
|
delete data.required;
|
|
464
|
-
data =
|
|
668
|
+
data = { required: param, ...data };
|
|
669
|
+
}
|
|
670
|
+
// and remote at the end: a pending request stops the other rules
|
|
671
|
+
if ('remote' in data) {
|
|
672
|
+
const param = data.remote;
|
|
673
|
+
delete data.remote;
|
|
674
|
+
data.remote = param;
|
|
465
675
|
}
|
|
466
676
|
return data;
|
|
467
677
|
}
|
|
@@ -487,19 +697,37 @@
|
|
|
487
697
|
}
|
|
488
698
|
/**
|
|
489
699
|
* Normalizes and processes validation rules for an element.
|
|
490
|
-
* Removes false rules,
|
|
700
|
+
* Removes false rules, applies conditional rules (`{ param, depends }`),
|
|
701
|
+
* evaluates function parameters, and converts string/number values.
|
|
491
702
|
* @param rules - raw validation rules object
|
|
703
|
+
* @param element - the element the rules are for
|
|
492
704
|
* @return normalized rules object
|
|
493
705
|
*/
|
|
494
|
-
function normalizeRules(rules) {
|
|
706
|
+
function normalizeRules(rules, element) {
|
|
495
707
|
Object.entries(rules).forEach(([key, value]) => {
|
|
496
|
-
// Ignore rule when param is explicitly false, eg. required:false
|
|
497
|
-
|
|
708
|
+
// Ignore rule when param is explicitly false, eg. required:false,
|
|
709
|
+
// or missing, eg. an equalto whose other field isn't in the form
|
|
710
|
+
if (value === false || value === null || value === undefined) {
|
|
498
711
|
delete rules[key];
|
|
499
712
|
return;
|
|
500
713
|
}
|
|
501
|
-
|
|
502
|
-
|
|
714
|
+
// { param, depends }: the rule applies only when depends holds
|
|
715
|
+
if (typeof value === 'object' &&
|
|
716
|
+
!(value instanceof Element) &&
|
|
717
|
+
('param' in value || 'depends' in value)) {
|
|
718
|
+
const keep = !('depends' in value) || depend(value.depends, element);
|
|
719
|
+
if (keep) {
|
|
720
|
+
rules[key] = value.param !== undefined ? value.param : true;
|
|
721
|
+
}
|
|
722
|
+
else {
|
|
723
|
+
delete rules[key];
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
// a function parameter is called with the element, eg. required: () => …
|
|
728
|
+
Object.keys(rules).forEach((key) => {
|
|
729
|
+
if (typeof rules[key] === 'function') {
|
|
730
|
+
rules[key] = rules[key].call(element, element);
|
|
503
731
|
}
|
|
504
732
|
});
|
|
505
733
|
// Clean number parameters
|
|
@@ -539,7 +767,7 @@
|
|
|
539
767
|
.map((className) => className.toLowerCase())
|
|
540
768
|
.forEach((className) => {
|
|
541
769
|
if (className in classRuleSettings) {
|
|
542
|
-
rules =
|
|
770
|
+
rules = { ...rules, ...classRuleSettings[className] };
|
|
543
771
|
}
|
|
544
772
|
});
|
|
545
773
|
}
|
|
@@ -580,14 +808,13 @@
|
|
|
580
808
|
* @return validation rules derived from data attributes
|
|
581
809
|
*/
|
|
582
810
|
function dataRules(element) {
|
|
583
|
-
var _a;
|
|
584
811
|
const rules = {};
|
|
585
812
|
const type = element.type;
|
|
586
813
|
for (const method of store$1.keys()) {
|
|
587
814
|
const datasetKey = 'rule' +
|
|
588
815
|
method.charAt(0).toUpperCase() +
|
|
589
816
|
method.substring(1).toLowerCase();
|
|
590
|
-
let value =
|
|
817
|
+
let value = element.dataset[datasetKey] ?? null;
|
|
591
818
|
// Cast empty attributes like `data-rule-required` to `true`
|
|
592
819
|
if (value === '' || value === 'true') {
|
|
593
820
|
value = true;
|
|
@@ -677,6 +904,8 @@
|
|
|
677
904
|
regex: 'Please enter a value that matches the pattern {0}.',
|
|
678
905
|
nonalphamin: 'Please enter at least {0} non-alphabetic characters.',
|
|
679
906
|
creditcard: 'Please enter a valid credit card number.',
|
|
907
|
+
extension: 'Please enter a value with a valid extension.',
|
|
908
|
+
remote: 'Please fix this field.',
|
|
680
909
|
});
|
|
681
910
|
/**
|
|
682
911
|
* Gets the appropriate error message for a validation rule failure.
|
|
@@ -688,13 +917,17 @@
|
|
|
688
917
|
* @return formatted error message
|
|
689
918
|
*/
|
|
690
919
|
function getMessage(element, rule, ignoreTitle, customMessage) {
|
|
691
|
-
|
|
920
|
+
const message = [
|
|
692
921
|
customMessage,
|
|
693
922
|
customDataMessage(element, rule.method),
|
|
694
923
|
(!ignoreTitle && element.title) || undefined,
|
|
695
924
|
store.get(rule.method),
|
|
696
925
|
`<strong>Warning: No message defined for ${element.name}</strong>`,
|
|
697
926
|
].find((x) => x !== undefined);
|
|
927
|
+
// a function message is called with the parameters and the element
|
|
928
|
+
if (typeof message === 'function') {
|
|
929
|
+
return String(message.call(this, rule.parameters, element));
|
|
930
|
+
}
|
|
698
931
|
const regex = /\$?\{(\d+)}/g;
|
|
699
932
|
if (/\$?\{(\d+)}/g.test(message)) {
|
|
700
933
|
return format(message.replace(regex, '{$1}'), rule.parameters);
|
|
@@ -712,7 +945,7 @@
|
|
|
712
945
|
*/
|
|
713
946
|
function customDataMessage(element, method) {
|
|
714
947
|
const dataSetKey = 'msg' + method.charAt(0).toUpperCase() + method.substring(1).toLowerCase();
|
|
715
|
-
if (!
|
|
948
|
+
if (!element?.dataset) {
|
|
716
949
|
return;
|
|
717
950
|
}
|
|
718
951
|
return element.dataset[dataSetKey] || element.dataset['msg'];
|
|
@@ -726,6 +959,8 @@
|
|
|
726
959
|
const EVENT_TARGETS = {
|
|
727
960
|
FOCUS_TARGETS: [
|
|
728
961
|
"[type='text']",
|
|
962
|
+
// an <input> without a type is a text input
|
|
963
|
+
'input:not([type])',
|
|
729
964
|
"[type='password']",
|
|
730
965
|
"[type='file']",
|
|
731
966
|
'select',
|
|
@@ -755,19 +990,22 @@
|
|
|
755
990
|
* Handles attaching and removing event listeners with proper delegation logic.
|
|
756
991
|
*/
|
|
757
992
|
class FormEventManager {
|
|
758
|
-
constructor(form, settings, validateCallback, focusInvalidCallback, shouldIgnoreElement
|
|
993
|
+
constructor(form, settings, validateCallback, focusInvalidCallback, shouldIgnoreElement,
|
|
994
|
+
/** holds the submit while async validations run; true if held */
|
|
995
|
+
holdSubmitCallback = () => false) {
|
|
759
996
|
this.form = form;
|
|
760
997
|
this.settings = settings;
|
|
761
998
|
this.validateCallback = validateCallback;
|
|
762
999
|
this.focusInvalidCallback = focusInvalidCallback;
|
|
763
1000
|
this.shouldIgnoreElement = shouldIgnoreElement;
|
|
1001
|
+
this.holdSubmitCallback = holdSubmitCallback;
|
|
764
1002
|
this.boundEventHandlers = {
|
|
765
1003
|
onFocusIn: null,
|
|
766
1004
|
onFocusOut: null,
|
|
767
1005
|
onKeyUp: null,
|
|
768
1006
|
onClick: null,
|
|
769
|
-
onClickSubmit: null,
|
|
770
1007
|
onSubmit: null,
|
|
1008
|
+
onInvalidForm: null,
|
|
771
1009
|
};
|
|
772
1010
|
this.submitButton = null;
|
|
773
1011
|
}
|
|
@@ -784,19 +1022,18 @@
|
|
|
784
1022
|
this.form.addEventListener('keyup', this.boundEventHandlers.onKeyUp);
|
|
785
1023
|
this.form.addEventListener('click', this.boundEventHandlers.onClick);
|
|
786
1024
|
if (this.settings.onsubmit) {
|
|
787
|
-
this.boundEventHandlers.onClickSubmit = this.handleSubmitClick;
|
|
788
1025
|
this.boundEventHandlers.onSubmit = (e) => this.handleSubmitForm(e);
|
|
789
|
-
this.form.addEventListener('click', this.boundEventHandlers.onClick);
|
|
790
1026
|
this.form.addEventListener('submit', this.boundEventHandlers.onSubmit);
|
|
791
1027
|
}
|
|
1028
|
+
// the validator wraps it to pass itself as the second argument
|
|
792
1029
|
const invalidHandler = this.settings.invalidHandler;
|
|
793
1030
|
if (invalidHandler) {
|
|
794
|
-
this.
|
|
795
|
-
if (
|
|
796
|
-
|
|
1031
|
+
this.boundEventHandlers.onInvalidForm = (e) => {
|
|
1032
|
+
if (this.isCustomEvent(e)) {
|
|
1033
|
+
invalidHandler(e);
|
|
797
1034
|
}
|
|
798
|
-
|
|
799
|
-
|
|
1035
|
+
};
|
|
1036
|
+
this.form.addEventListener('invalid-form', this.boundEventHandlers.onInvalidForm);
|
|
800
1037
|
}
|
|
801
1038
|
}
|
|
802
1039
|
triggerInvalidForm(errorList) {
|
|
@@ -809,29 +1046,42 @@
|
|
|
809
1046
|
isCustomEvent(event) {
|
|
810
1047
|
return 'detail' in event;
|
|
811
1048
|
}
|
|
812
|
-
handleSubmitClick(e) {
|
|
813
|
-
const target = e.target;
|
|
814
|
-
// Track the used submit button to properly handle scripted
|
|
815
|
-
// submits later.
|
|
816
|
-
if (target.matches('button[type="submit"], input[type="submit"]')) {
|
|
817
|
-
this.submitButton = e.target;
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
1049
|
handleSubmitForm(e) {
|
|
821
1050
|
if (this.settings.debug) {
|
|
822
1051
|
e.preventDefault();
|
|
823
1052
|
}
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
1053
|
+
// the button used to submit, so its name/value can be sent along
|
|
1054
|
+
const submitter = e.submitter;
|
|
1055
|
+
this.submitButton =
|
|
1056
|
+
submitter instanceof HTMLButtonElement ||
|
|
1057
|
+
submitter instanceof HTMLInputElement
|
|
1058
|
+
? submitter
|
|
1059
|
+
: null;
|
|
1060
|
+
// a button with formnovalidate or class="cancel" (Cancel, Back, Save
|
|
1061
|
+
// draft) submits without validation, but still runs the submitHandler
|
|
1062
|
+
const skipValidation = !!this.submitButton &&
|
|
1063
|
+
(this.submitButton.hasAttribute('formnovalidate') ||
|
|
1064
|
+
this.submitButton.classList.contains('cancel'));
|
|
1065
|
+
if (!skipValidation) {
|
|
1066
|
+
const isValid = this.validateCallback();
|
|
1067
|
+
if (!isValid) {
|
|
1068
|
+
this.focusInvalidCallback();
|
|
1069
|
+
e.preventDefault();
|
|
1070
|
+
e.stopPropagation();
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
1073
|
+
// async validations (e.g. remote) are running: wait for them, then
|
|
1074
|
+
// the validator submits again if everything is valid
|
|
1075
|
+
if (this.holdSubmitCallback(submitter ?? null)) {
|
|
1076
|
+
e.preventDefault();
|
|
1077
|
+
e.stopPropagation();
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
830
1080
|
}
|
|
831
1081
|
let hidden = null;
|
|
832
1082
|
// Insert a hidden input as a replacement for the missing submit button
|
|
833
1083
|
// if a submitHandler is passed as option
|
|
834
|
-
if (this.submitButton && this.settings.submitHandler) {
|
|
1084
|
+
if (this.submitButton?.name && this.settings.submitHandler) {
|
|
835
1085
|
hidden = document.createElement('input');
|
|
836
1086
|
hidden.setAttribute('type', 'hidden');
|
|
837
1087
|
hidden.setAttribute('name', this.submitButton.name);
|
|
@@ -866,19 +1116,19 @@
|
|
|
866
1116
|
if (this.boundEventHandlers.onClick) {
|
|
867
1117
|
this.form.removeEventListener('click', this.boundEventHandlers.onClick);
|
|
868
1118
|
}
|
|
869
|
-
if (this.boundEventHandlers.onClickSubmit) {
|
|
870
|
-
this.form.removeEventListener('click', this.boundEventHandlers.onClickSubmit);
|
|
871
|
-
}
|
|
872
1119
|
if (this.boundEventHandlers.onSubmit) {
|
|
873
1120
|
this.form.removeEventListener('submit', this.boundEventHandlers.onSubmit);
|
|
874
1121
|
}
|
|
1122
|
+
if (this.boundEventHandlers.onInvalidForm) {
|
|
1123
|
+
this.form.removeEventListener('invalid-form', this.boundEventHandlers.onInvalidForm);
|
|
1124
|
+
}
|
|
875
1125
|
// Clear references
|
|
876
1126
|
this.boundEventHandlers.onFocusIn = null;
|
|
877
1127
|
this.boundEventHandlers.onFocusOut = null;
|
|
878
1128
|
this.boundEventHandlers.onKeyUp = null;
|
|
879
1129
|
this.boundEventHandlers.onClick = null;
|
|
880
|
-
this.boundEventHandlers.onClickSubmit = null;
|
|
881
1130
|
this.boundEventHandlers.onSubmit = null;
|
|
1131
|
+
this.boundEventHandlers.onInvalidForm = null;
|
|
882
1132
|
}
|
|
883
1133
|
/**
|
|
884
1134
|
* Creates a delegated event handler for the specified targets and handler
|
|
@@ -926,6 +1176,7 @@
|
|
|
926
1176
|
ignore: ':hidden',
|
|
927
1177
|
errorClass: 'error',
|
|
928
1178
|
validClass: 'valid',
|
|
1179
|
+
pendingClass: 'pending',
|
|
929
1180
|
errorElement: 'label',
|
|
930
1181
|
wrapper: null,
|
|
931
1182
|
errorLabelContainer: null,
|
|
@@ -1031,6 +1282,40 @@
|
|
|
1031
1282
|
});
|
|
1032
1283
|
}
|
|
1033
1284
|
|
|
1285
|
+
function isPromiseLike(value) {
|
|
1286
|
+
return !!value && typeof value.then === 'function';
|
|
1287
|
+
}
|
|
1288
|
+
/** Identifies the input of an async method: its values and parameters. */
|
|
1289
|
+
function asyncKey(values, param) {
|
|
1290
|
+
try {
|
|
1291
|
+
return JSON.stringify([values, param], (_key, value) => typeof value === 'function' ? value() : value);
|
|
1292
|
+
}
|
|
1293
|
+
catch {
|
|
1294
|
+
return JSON.stringify(values);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
/**
|
|
1298
|
+
* Methods that, like jQuery's, pass a blank field as "optional" (jQuery's
|
|
1299
|
+
* "dependency-mismatch"). When one is a blank field's only rule, the field is
|
|
1300
|
+
* neutral: neither an error nor a success.
|
|
1301
|
+
*/
|
|
1302
|
+
const OPTIONAL_METHODS = new Set([
|
|
1303
|
+
'minlength',
|
|
1304
|
+
'maxlength',
|
|
1305
|
+
'rangelength',
|
|
1306
|
+
'min',
|
|
1307
|
+
'max',
|
|
1308
|
+
'range',
|
|
1309
|
+
'email',
|
|
1310
|
+
'url',
|
|
1311
|
+
'date',
|
|
1312
|
+
'unobtrusivedate',
|
|
1313
|
+
'number',
|
|
1314
|
+
'digits',
|
|
1315
|
+
'creditcard',
|
|
1316
|
+
'extension',
|
|
1317
|
+
'remote',
|
|
1318
|
+
]);
|
|
1034
1319
|
class Validator {
|
|
1035
1320
|
/**
|
|
1036
1321
|
* Initializes a new validator instance for the given form with optional configuration settings.
|
|
@@ -1039,6 +1324,7 @@
|
|
|
1039
1324
|
*/
|
|
1040
1325
|
constructor(form, options) {
|
|
1041
1326
|
this.submitted = {};
|
|
1327
|
+
/** fields validated so far: their message, or false when valid */
|
|
1042
1328
|
this.invalid = {};
|
|
1043
1329
|
this.successList = [];
|
|
1044
1330
|
this.errorList = [];
|
|
@@ -1051,9 +1337,27 @@
|
|
|
1051
1337
|
this.lastActive = null;
|
|
1052
1338
|
/** normalized rules */
|
|
1053
1339
|
this.rules = {};
|
|
1340
|
+
/** whether init() added the novalidate attribute (so destroy() removes it) */
|
|
1341
|
+
this.addedNoValidate = false;
|
|
1342
|
+
/** number of async validations (e.g. remote) still running */
|
|
1343
|
+
this.pendingRequest = 0;
|
|
1344
|
+
/** running async validations by field name */
|
|
1345
|
+
this.pending = {};
|
|
1346
|
+
/** cached async results by field name, then method */
|
|
1347
|
+
this.asyncResults = {};
|
|
1348
|
+
/** a submit is waiting for the running async validations */
|
|
1349
|
+
this.formSubmitted = false;
|
|
1350
|
+
/** the button that started the held submit */
|
|
1351
|
+
this.heldSubmitter = null;
|
|
1352
|
+
/** while > 0, cached failed requests are reused instead of retried */
|
|
1353
|
+
this.reuseFailed = 0;
|
|
1354
|
+
/** fields whose equalto rule has run: rechecked when the other field blurs */
|
|
1355
|
+
this.equalToChecked = new Set();
|
|
1356
|
+
/** callbacks waiting for pendingRequest to reach 0 */
|
|
1357
|
+
this.settleWaiters = [];
|
|
1054
1358
|
this.settings = ValidatorDefaults;
|
|
1055
1359
|
this.currentForm = form;
|
|
1056
|
-
this.settings =
|
|
1360
|
+
this.settings = { ...this.settings, ...options };
|
|
1057
1361
|
this.normalizeSettings();
|
|
1058
1362
|
this.init();
|
|
1059
1363
|
}
|
|
@@ -1071,7 +1375,6 @@
|
|
|
1071
1375
|
'highlight',
|
|
1072
1376
|
'unhighlight',
|
|
1073
1377
|
'errorPlacement',
|
|
1074
|
-
'invalidHandler',
|
|
1075
1378
|
'submitHandler',
|
|
1076
1379
|
].forEach((key) => {
|
|
1077
1380
|
const setting = this.settings[key];
|
|
@@ -1079,6 +1382,14 @@
|
|
|
1079
1382
|
this.settings[key] = setting.bind(this);
|
|
1080
1383
|
}
|
|
1081
1384
|
});
|
|
1385
|
+
// like jQuery: invalidHandler(event, validator)
|
|
1386
|
+
const invalidHandler = this.settings.invalidHandler;
|
|
1387
|
+
if (invalidHandler) {
|
|
1388
|
+
this.settings.invalidHandler = (event) => invalidHandler.call(this, event, this);
|
|
1389
|
+
}
|
|
1390
|
+
// give each validator its own messages map, so setRules() never writes
|
|
1391
|
+
// into the shared defaults or the caller's options object
|
|
1392
|
+
this.settings.messages = { ...this.settings.messages };
|
|
1082
1393
|
// transform method names to lowercase in messages
|
|
1083
1394
|
if (this.settings.messages) {
|
|
1084
1395
|
Object.keys(this.settings.messages).forEach((elementName) => {
|
|
@@ -1099,22 +1410,68 @@
|
|
|
1099
1410
|
* Sets up DOM references and attaches event handlers to the form.
|
|
1100
1411
|
*/
|
|
1101
1412
|
init() {
|
|
1413
|
+
// turn off the browser's own validation (popups, blocked submits),
|
|
1414
|
+
// which would otherwise run before ours for required, type="email", etc.
|
|
1415
|
+
if (!this.currentForm.hasAttribute('novalidate')) {
|
|
1416
|
+
this.currentForm.setAttribute('novalidate', 'novalidate');
|
|
1417
|
+
this.addedNoValidate = true;
|
|
1418
|
+
}
|
|
1102
1419
|
if (this.settings.errorLabelContainer) {
|
|
1103
1420
|
this.labelContainer = document.querySelector(this.settings.errorLabelContainer);
|
|
1104
1421
|
}
|
|
1105
1422
|
this.errorContext = this.labelContainer || this.currentForm;
|
|
1423
|
+
// every element errorContainer matches is shown and hidden; error labels
|
|
1424
|
+
// only go in the first element errorLabelContainer matches
|
|
1106
1425
|
const containers = [this.labelContainer];
|
|
1107
1426
|
if (this.settings.errorContainer) {
|
|
1108
|
-
containers.push(document.
|
|
1427
|
+
containers.push(...document.querySelectorAll(this.settings.errorContainer));
|
|
1109
1428
|
}
|
|
1110
1429
|
this.containers = containers.filter(Boolean);
|
|
1111
1430
|
this.rules = {};
|
|
1112
1431
|
Object.entries(this.settings.rules).forEach(([key, value]) => {
|
|
1113
1432
|
this.rules[key] = normalizeRule(value);
|
|
1114
1433
|
});
|
|
1115
|
-
|
|
1434
|
+
// like jQuery's equalTo: leaving the other field rechecks this one
|
|
1435
|
+
const onfocusout = this.settings.onfocusout;
|
|
1436
|
+
if (onfocusout) {
|
|
1437
|
+
this.settings.onfocusout = (element, event) => {
|
|
1438
|
+
onfocusout(element, event);
|
|
1439
|
+
this.recheckEqualTo(element);
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
this.eventManager = new FormEventManager(this.currentForm, this.settings, () => this.form(), () => this.focusInvalid(), (element) => this.shouldIgnore(element), (submitter) => this.holdSubmit(submitter));
|
|
1116
1443
|
this.eventManager.attachEventHandlers();
|
|
1117
1444
|
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Sets the rules (and optionally the messages) for a field by name after
|
|
1447
|
+
* the validator has been created, replacing any rules set for it before.
|
|
1448
|
+
* Useful when fields are added to the form dynamically.
|
|
1449
|
+
* Rules from HTML attributes, data attributes and classes still apply.
|
|
1450
|
+
* @param name - name attribute of the field
|
|
1451
|
+
* @param rules - rules for the field, or null to remove them
|
|
1452
|
+
* @param messages - custom messages for the field, keyed by method name
|
|
1453
|
+
* @example
|
|
1454
|
+
* validator.setRules('email', { required: true, email: true }, {
|
|
1455
|
+
* required: 'We need your email.',
|
|
1456
|
+
* });
|
|
1457
|
+
*/
|
|
1458
|
+
setRules(name, rules, messages) {
|
|
1459
|
+
if (rules === null) {
|
|
1460
|
+
delete this.rules[name];
|
|
1461
|
+
delete this.settings.messages[name];
|
|
1462
|
+
return;
|
|
1463
|
+
}
|
|
1464
|
+
this.rules[name] = normalizeRule(rules);
|
|
1465
|
+
if (typeof messages === 'string') {
|
|
1466
|
+
this.settings.messages[name] = messages;
|
|
1467
|
+
}
|
|
1468
|
+
else if (messages) {
|
|
1469
|
+
this.settings.messages[name] = Object.fromEntries(Object.entries(messages).map(([method, msg]) => [
|
|
1470
|
+
method.toLowerCase(),
|
|
1471
|
+
msg,
|
|
1472
|
+
]));
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1118
1475
|
/**
|
|
1119
1476
|
* Resets the validator state.
|
|
1120
1477
|
*/
|
|
@@ -1131,17 +1488,19 @@
|
|
|
1131
1488
|
* @return true if the form is valid, false otherwise
|
|
1132
1489
|
*/
|
|
1133
1490
|
form() {
|
|
1491
|
+
this.retryFailedRequests();
|
|
1134
1492
|
this.prepareForm();
|
|
1135
1493
|
this.currentElements = this.elements();
|
|
1136
1494
|
for (const element of this.currentElements) {
|
|
1137
1495
|
this.check(element);
|
|
1138
1496
|
}
|
|
1139
|
-
this.submitted =
|
|
1140
|
-
this.invalid =
|
|
1497
|
+
this.submitted = { ...this.submitted, ...this.errorMap };
|
|
1498
|
+
this.invalid = { ...this.errorMap };
|
|
1141
1499
|
if (!this.valid()) {
|
|
1142
1500
|
this.eventManager.triggerInvalidForm(this.errorList);
|
|
1143
1501
|
}
|
|
1144
1502
|
this.showErrors();
|
|
1503
|
+
this.notifySettled();
|
|
1145
1504
|
return this.valid();
|
|
1146
1505
|
}
|
|
1147
1506
|
/**
|
|
@@ -1150,6 +1509,7 @@
|
|
|
1150
1509
|
* @return true if the element is valid, false otherwise
|
|
1151
1510
|
*/
|
|
1152
1511
|
element(element) {
|
|
1512
|
+
this.retryFailedRequests();
|
|
1153
1513
|
const target = this.validationTargetFor(element);
|
|
1154
1514
|
if (target === undefined) {
|
|
1155
1515
|
delete this.invalid[element.name];
|
|
@@ -1158,18 +1518,16 @@
|
|
|
1158
1518
|
this.prepareElement(target);
|
|
1159
1519
|
this.currentElements = [target];
|
|
1160
1520
|
const result = this.check(target);
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
else {
|
|
1165
|
-
this.invalid[target.name] = 'Invalid';
|
|
1166
|
-
}
|
|
1521
|
+
// keep valid fields as false (not deleted): onkeyup revalidates every
|
|
1522
|
+
// field that has been validated once, as jQuery does
|
|
1523
|
+
this.invalid[target.name] = result ? false : 'Invalid';
|
|
1167
1524
|
if (!this.numberOfInvalids()) {
|
|
1168
1525
|
this.toHide.push(...this.containers);
|
|
1169
1526
|
}
|
|
1170
1527
|
this.showErrors();
|
|
1171
1528
|
// add aria-invalid status for screen readers
|
|
1172
1529
|
element.setAttribute('aria-invalid', (!result).toString());
|
|
1530
|
+
this.notifySettled();
|
|
1173
1531
|
return result;
|
|
1174
1532
|
}
|
|
1175
1533
|
prepareElement(element) {
|
|
@@ -1197,9 +1555,11 @@
|
|
|
1197
1555
|
elements() {
|
|
1198
1556
|
const rulesCache = {};
|
|
1199
1557
|
const selector = 'input, select, textarea';
|
|
1200
|
-
|
|
1558
|
+
// :disabled also matches fields inside <fieldset disabled>
|
|
1559
|
+
const notSelector = 'input[type="submit"], [type="reset"], [type="image"], :disabled';
|
|
1201
1560
|
const elements = Array.from(this.currentForm.querySelectorAll(selector)).filter((el) => !el.matches(notSelector) && !this.shouldIgnore(el));
|
|
1202
|
-
|
|
1561
|
+
// getAttribute: a field named "id" shadows form.id
|
|
1562
|
+
const formId = this.currentForm.getAttribute('id');
|
|
1203
1563
|
if (formId) {
|
|
1204
1564
|
const escapedId = escapeCssMeta(formId);
|
|
1205
1565
|
const outsideElements = Array.from(document.querySelectorAll(selector)).filter((el) => el.matches(`[form="${escapedId}"]`) &&
|
|
@@ -1228,7 +1588,8 @@
|
|
|
1228
1588
|
*/
|
|
1229
1589
|
validElements() {
|
|
1230
1590
|
const invalid = this.invalidElements();
|
|
1231
|
-
|
|
1591
|
+
// fields with a running async validation are not known to be valid yet
|
|
1592
|
+
return this.currentElements.filter((el) => !invalid.includes(el) && !(el.name in this.pending));
|
|
1232
1593
|
}
|
|
1233
1594
|
/**
|
|
1234
1595
|
* Returns an array of currently invalid elements from the last validation.
|
|
@@ -1243,14 +1604,24 @@
|
|
|
1243
1604
|
* @return true if the element is valid, false otherwise
|
|
1244
1605
|
*/
|
|
1245
1606
|
check(element) {
|
|
1246
|
-
var _a;
|
|
1247
1607
|
element = this.validationTargetFor(element);
|
|
1248
1608
|
const rules = getRules(element, this.rules);
|
|
1249
1609
|
const elValue = elementValue(element, this.currentForm);
|
|
1250
|
-
const value = (
|
|
1610
|
+
const value = (Array.isArray(elValue) ? elValue[0] : elValue) ?? '';
|
|
1251
1611
|
const values = Array.isArray(elValue) ? elValue : [elValue];
|
|
1252
1612
|
const length = getValueLength(element, this.currentForm);
|
|
1253
1613
|
const blank = isBlankElement(element, this.currentForm);
|
|
1614
|
+
// a running async validation for a previous value is out of date
|
|
1615
|
+
const running = this.pending[element.name];
|
|
1616
|
+
if (running &&
|
|
1617
|
+
(!(running.method in rules) ||
|
|
1618
|
+
running.key !== asyncKey(values, rules[running.method]))) {
|
|
1619
|
+
this.abortRequest(element);
|
|
1620
|
+
}
|
|
1621
|
+
// a lone rule that doesn't apply (blank optional field, or required
|
|
1622
|
+
// whose condition is false) leaves the field neutral: no success
|
|
1623
|
+
const ruleCount = Object.keys(rules).length;
|
|
1624
|
+
let neutral = false;
|
|
1254
1625
|
for (const method in rules) {
|
|
1255
1626
|
const rule = { method, parameters: rules[method] };
|
|
1256
1627
|
const methodFunc = store$1.get(method);
|
|
@@ -1258,7 +1629,25 @@
|
|
|
1258
1629
|
console.warn(`Validation method "${method}" not found. Skipping.`);
|
|
1259
1630
|
continue;
|
|
1260
1631
|
}
|
|
1261
|
-
|
|
1632
|
+
// async methods: reuse the result while the value is unchanged
|
|
1633
|
+
const cached = this.asyncResults[element.name]?.[method];
|
|
1634
|
+
if (cached && cached.key === asyncKey(values, rule.parameters)) {
|
|
1635
|
+
if (cached.result === true) {
|
|
1636
|
+
continue;
|
|
1637
|
+
}
|
|
1638
|
+
// like jQuery, remote's own message is formatted with the value
|
|
1639
|
+
// ("'{0}' is already taken"), not with the request options
|
|
1640
|
+
this.formatAndAdd(element, rule, cached.result ??
|
|
1641
|
+
(method === 'remote'
|
|
1642
|
+
? this.getMessage(element, { method, parameters: value })
|
|
1643
|
+
: undefined));
|
|
1644
|
+
return false;
|
|
1645
|
+
}
|
|
1646
|
+
if (method === 'equalto') {
|
|
1647
|
+
this.equalToChecked.add(element.name);
|
|
1648
|
+
}
|
|
1649
|
+
if (this.pending[element.name]?.method !== method) {
|
|
1650
|
+
const controller = new AbortController();
|
|
1262
1651
|
const result = methodFunc({
|
|
1263
1652
|
blank,
|
|
1264
1653
|
value: value,
|
|
@@ -1266,21 +1655,189 @@
|
|
|
1266
1655
|
length,
|
|
1267
1656
|
element,
|
|
1268
1657
|
param: rule.parameters,
|
|
1658
|
+
signal: controller.signal,
|
|
1269
1659
|
});
|
|
1270
|
-
if (!result) {
|
|
1271
|
-
|
|
1272
|
-
|
|
1660
|
+
if (!isPromiseLike(result)) {
|
|
1661
|
+
if (!result) {
|
|
1662
|
+
this.formatAndAdd(element, rule);
|
|
1663
|
+
return false;
|
|
1664
|
+
}
|
|
1665
|
+
neutral =
|
|
1666
|
+
ruleCount === 1 &&
|
|
1667
|
+
(method === 'required'
|
|
1668
|
+
? !depend(rule.parameters, element)
|
|
1669
|
+
: blank && OPTIONAL_METHODS.has(method));
|
|
1670
|
+
continue;
|
|
1273
1671
|
}
|
|
1672
|
+
const key = asyncKey(values, rule.parameters);
|
|
1673
|
+
this.startRequest(element, { controller, method, key });
|
|
1674
|
+
result.then((response) => this.finishRequest(element, key, controller, response), () => this.finishRequest(element, key, controller, null));
|
|
1274
1675
|
}
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1676
|
+
// Pending: not an error (yet). Keep the current message visible and
|
|
1677
|
+
// stop here; the field is validated again when the answer arrives.
|
|
1678
|
+
const shown = this.errorsFor(element);
|
|
1679
|
+
this.toHide = this.toHide.filter((el) => !shown.includes(el));
|
|
1680
|
+
return true;
|
|
1278
1681
|
}
|
|
1279
|
-
if (objectLength(rules)) {
|
|
1682
|
+
if (!neutral && objectLength(rules)) {
|
|
1280
1683
|
this.successList.push(element);
|
|
1281
1684
|
}
|
|
1282
1685
|
return true;
|
|
1283
1686
|
}
|
|
1687
|
+
/**
|
|
1688
|
+
* Validates again the fields whose equalto rule points at the element
|
|
1689
|
+
* (and has run before), e.g. ConfirmPassword when Password changes.
|
|
1690
|
+
*/
|
|
1691
|
+
recheckEqualTo(element) {
|
|
1692
|
+
this.equalToChecked.forEach((name) => {
|
|
1693
|
+
const field = findByName(this.currentForm, name)[0];
|
|
1694
|
+
if (!field || field === element) {
|
|
1695
|
+
return;
|
|
1696
|
+
}
|
|
1697
|
+
const param = getRules(field, this.rules)['equalto'];
|
|
1698
|
+
let target = null;
|
|
1699
|
+
try {
|
|
1700
|
+
target =
|
|
1701
|
+
param instanceof Element ? param : document.querySelector(param);
|
|
1702
|
+
}
|
|
1703
|
+
catch {
|
|
1704
|
+
// not a selector: the equalto rule itself reports it
|
|
1705
|
+
}
|
|
1706
|
+
if (target === element) {
|
|
1707
|
+
this.element(field);
|
|
1708
|
+
}
|
|
1709
|
+
});
|
|
1710
|
+
}
|
|
1711
|
+
/**
|
|
1712
|
+
* Returns a promise that resolves once no async validations are running.
|
|
1713
|
+
*/
|
|
1714
|
+
settled() {
|
|
1715
|
+
if (this.pendingRequest === 0) {
|
|
1716
|
+
return Promise.resolve();
|
|
1717
|
+
}
|
|
1718
|
+
return new Promise((resolve) => this.settleWaiters.push(resolve));
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Runs fn reusing the results of failed requests instead of retrying them.
|
|
1722
|
+
* @internal
|
|
1723
|
+
*/
|
|
1724
|
+
withCachedResults(fn) {
|
|
1725
|
+
this.reuseFailed++;
|
|
1726
|
+
try {
|
|
1727
|
+
return fn();
|
|
1728
|
+
}
|
|
1729
|
+
finally {
|
|
1730
|
+
this.reuseFailed--;
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
retryFailedRequests() {
|
|
1734
|
+
if (this.reuseFailed) {
|
|
1735
|
+
return;
|
|
1736
|
+
}
|
|
1737
|
+
Object.values(this.asyncResults).forEach((byMethod) => {
|
|
1738
|
+
Object.keys(byMethod).forEach((method) => {
|
|
1739
|
+
if (byMethod[method].failed) {
|
|
1740
|
+
delete byMethod[method];
|
|
1741
|
+
}
|
|
1742
|
+
});
|
|
1743
|
+
});
|
|
1744
|
+
}
|
|
1745
|
+
startRequest(element, request) {
|
|
1746
|
+
this.abortRequest(element);
|
|
1747
|
+
this.pending[element.name] = request;
|
|
1748
|
+
this.pendingRequest++;
|
|
1749
|
+
if (this.settings.pendingClass) {
|
|
1750
|
+
element.classList.add(this.settings.pendingClass);
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
abortRequest(element) {
|
|
1754
|
+
const running = this.pending[element.name];
|
|
1755
|
+
if (!running) {
|
|
1756
|
+
return;
|
|
1757
|
+
}
|
|
1758
|
+
running.controller.abort();
|
|
1759
|
+
this.endRequest(element);
|
|
1760
|
+
// the user changed the field while a submit was waiting: drop the submit
|
|
1761
|
+
if (this.pendingRequest === 0) {
|
|
1762
|
+
this.formSubmitted = false;
|
|
1763
|
+
this.heldSubmitter = null;
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
endRequest(element) {
|
|
1767
|
+
delete this.pending[element.name];
|
|
1768
|
+
this.pendingRequest = Math.max(0, this.pendingRequest - 1);
|
|
1769
|
+
if (this.settings.pendingClass) {
|
|
1770
|
+
element.classList.remove(this.settings.pendingClass);
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
finishRequest(element, key, controller, response) {
|
|
1774
|
+
var _a, _b;
|
|
1775
|
+
// a newer validation, a reset or destroy() replaced this one
|
|
1776
|
+
const running = this.pending[element.name];
|
|
1777
|
+
if (controller.signal.aborted || running?.controller !== controller) {
|
|
1778
|
+
return;
|
|
1779
|
+
}
|
|
1780
|
+
this.endRequest(element);
|
|
1781
|
+
const valid = response === true || response === 'true';
|
|
1782
|
+
((_a = this.asyncResults)[_b = element.name] ?? (_a[_b] = {}))[running.method] = {
|
|
1783
|
+
key,
|
|
1784
|
+
result: valid
|
|
1785
|
+
? true
|
|
1786
|
+
: typeof response === 'string' && response !== ''
|
|
1787
|
+
? response
|
|
1788
|
+
: null,
|
|
1789
|
+
failed: response === null,
|
|
1790
|
+
};
|
|
1791
|
+
// validate the field again: the answer is cached now, and any rules
|
|
1792
|
+
// after the async one run too (unless the field or validator is gone)
|
|
1793
|
+
if (validatorStore.get(this.currentForm) === this && element.isConnected) {
|
|
1794
|
+
this.withCachedResults(() => this.element(element));
|
|
1795
|
+
}
|
|
1796
|
+
if (this.pendingRequest === 0) {
|
|
1797
|
+
this.resumeSubmit();
|
|
1798
|
+
this.notifySettled();
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
/** Called on submit while async validations are running. */
|
|
1802
|
+
holdSubmit(submitter) {
|
|
1803
|
+
if (this.pendingRequest === 0) {
|
|
1804
|
+
return false;
|
|
1805
|
+
}
|
|
1806
|
+
this.formSubmitted = true;
|
|
1807
|
+
this.heldSubmitter = submitter;
|
|
1808
|
+
return true;
|
|
1809
|
+
}
|
|
1810
|
+
/** Submits the held submit once everything is valid, as the user meant to. */
|
|
1811
|
+
resumeSubmit() {
|
|
1812
|
+
if (!this.formSubmitted) {
|
|
1813
|
+
return;
|
|
1814
|
+
}
|
|
1815
|
+
this.formSubmitted = false;
|
|
1816
|
+
const submitter = this.heldSubmitter;
|
|
1817
|
+
this.heldSubmitter = null;
|
|
1818
|
+
this.withCachedResults(() => {
|
|
1819
|
+
if (!this.form()) {
|
|
1820
|
+
this.focusInvalid();
|
|
1821
|
+
return;
|
|
1822
|
+
}
|
|
1823
|
+
if (this.holdSubmit(submitter)) {
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1826
|
+
const form = this.currentForm;
|
|
1827
|
+
const isSubmitter = submitter instanceof HTMLElement &&
|
|
1828
|
+
submitter.form === form &&
|
|
1829
|
+
submitter.matches('button[type="submit"], button:not([type]), input[type="submit"], input[type="image"]');
|
|
1830
|
+
form.requestSubmit(isSubmitter ? submitter : undefined);
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1833
|
+
notifySettled() {
|
|
1834
|
+
if (this.pendingRequest !== 0) {
|
|
1835
|
+
return;
|
|
1836
|
+
}
|
|
1837
|
+
const waiters = this.settleWaiters;
|
|
1838
|
+
this.settleWaiters = [];
|
|
1839
|
+
waiters.forEach((resolve) => resolve());
|
|
1840
|
+
}
|
|
1284
1841
|
/**
|
|
1285
1842
|
* Returns all error label elements currently in the DOM.
|
|
1286
1843
|
* @return array of error label elements
|
|
@@ -1292,7 +1849,7 @@
|
|
|
1292
1849
|
showErrors(errors) {
|
|
1293
1850
|
if (errors) {
|
|
1294
1851
|
// Add items to error list and map
|
|
1295
|
-
this.errorMap =
|
|
1852
|
+
this.errorMap = { ...this.errorMap, ...errors };
|
|
1296
1853
|
this.errorList = Object.entries(this.errorMap).map(([name, message]) => ({
|
|
1297
1854
|
message,
|
|
1298
1855
|
method: message,
|
|
@@ -1330,8 +1887,7 @@
|
|
|
1330
1887
|
this.hideErrors();
|
|
1331
1888
|
this.addWrapper(...this.toShow).forEach(showElement);
|
|
1332
1889
|
}
|
|
1333
|
-
formatAndAdd(element, rule) {
|
|
1334
|
-
const message = this.getMessage(element, rule);
|
|
1890
|
+
formatAndAdd(element, rule, message = this.getMessage(element, rule)) {
|
|
1335
1891
|
this.errorList.push({ message, element, method: rule.method });
|
|
1336
1892
|
this.errorMap[element.name] = message;
|
|
1337
1893
|
this.submitted[element.name] = message;
|
|
@@ -1365,7 +1921,6 @@
|
|
|
1365
1921
|
return result;
|
|
1366
1922
|
}
|
|
1367
1923
|
showLabel(element, message) {
|
|
1368
|
-
var _a;
|
|
1369
1924
|
let errors = this.errorsFor(element);
|
|
1370
1925
|
const elementID = idOrName(element);
|
|
1371
1926
|
const describedBy = element.getAttribute('aria-describedby');
|
|
@@ -1376,11 +1931,10 @@
|
|
|
1376
1931
|
.map((e) => e.closest(labelSelector))
|
|
1377
1932
|
.every((e) => e === null);
|
|
1378
1933
|
const noDescriberAssociation = () => {
|
|
1379
|
-
var _a, _b;
|
|
1380
1934
|
if (describedBy === null) {
|
|
1381
1935
|
return true;
|
|
1382
1936
|
}
|
|
1383
|
-
const firstErrorId =
|
|
1937
|
+
const firstErrorId = errors[0]?.getAttribute('id') ?? null;
|
|
1384
1938
|
if (firstErrorId === null) {
|
|
1385
1939
|
return true;
|
|
1386
1940
|
}
|
|
@@ -1446,7 +2000,7 @@
|
|
|
1446
2000
|
}
|
|
1447
2001
|
// If the element is not a child of an associated label, then it's necessary
|
|
1448
2002
|
// to explicitly apply aria-describedby
|
|
1449
|
-
else if (!
|
|
2003
|
+
else if (!newError.parentElement?.matches(`label[for='${escapeCssMeta(elementID)}']`)) {
|
|
1450
2004
|
this.addErrorAriaDescribedBy(element, newError.getAttribute('id'));
|
|
1451
2005
|
}
|
|
1452
2006
|
errors = [newError];
|
|
@@ -1490,10 +2044,27 @@
|
|
|
1490
2044
|
this.resetForm();
|
|
1491
2045
|
validatorStore.delete(this.currentForm);
|
|
1492
2046
|
this.eventManager.detachEventHandlers();
|
|
2047
|
+
if (this.addedNoValidate) {
|
|
2048
|
+
this.currentForm.removeAttribute('novalidate');
|
|
2049
|
+
}
|
|
1493
2050
|
}
|
|
1494
2051
|
resetForm() {
|
|
2052
|
+
// stop running async validations and forget their results
|
|
2053
|
+
Object.entries(this.pending).forEach(([name, { controller }]) => {
|
|
2054
|
+
controller.abort();
|
|
2055
|
+
if (this.settings.pendingClass) {
|
|
2056
|
+
findByName(this.currentForm, name).forEach((el) => el.classList.remove(this.settings.pendingClass));
|
|
2057
|
+
}
|
|
2058
|
+
});
|
|
2059
|
+
this.pending = {};
|
|
2060
|
+
this.pendingRequest = 0;
|
|
2061
|
+
this.asyncResults = {};
|
|
2062
|
+
this.formSubmitted = false;
|
|
2063
|
+
this.heldSubmitter = null;
|
|
2064
|
+
this.notifySettled();
|
|
1495
2065
|
this.invalid = {};
|
|
1496
2066
|
this.submitted = {};
|
|
2067
|
+
this.equalToChecked.clear();
|
|
1497
2068
|
this.prepareForm();
|
|
1498
2069
|
this.hideErrors();
|
|
1499
2070
|
this.elements().forEach((el) => el.removeAttribute('aria-invalid'));
|
|
@@ -1526,13 +2097,14 @@
|
|
|
1526
2097
|
* @return true if the element should be skipped, false otherwise
|
|
1527
2098
|
*/
|
|
1528
2099
|
shouldIgnore(element) {
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
2100
|
+
// built again only when the setting changes
|
|
2101
|
+
if (!this.ignoreTest || this.ignoreTest[0] !== this.settings.ignore) {
|
|
2102
|
+
this.ignoreTest = [
|
|
2103
|
+
this.settings.ignore,
|
|
2104
|
+
ignoreMatcher(this.settings.ignore),
|
|
2105
|
+
];
|
|
1534
2106
|
}
|
|
1535
|
-
return
|
|
2107
|
+
return this.ignoreTest[1](element);
|
|
1536
2108
|
}
|
|
1537
2109
|
getMessage(element, rule) {
|
|
1538
2110
|
if (typeof rule === 'string') {
|
|
@@ -1639,7 +2211,6 @@
|
|
|
1639
2211
|
* }
|
|
1640
2212
|
*/
|
|
1641
2213
|
function valid(selector) {
|
|
1642
|
-
var _a, _b;
|
|
1643
2214
|
let elements = [];
|
|
1644
2215
|
if (typeof selector === 'string') {
|
|
1645
2216
|
elements = Array.from(document.querySelectorAll(selector));
|
|
@@ -1651,7 +2222,7 @@
|
|
|
1651
2222
|
elements = selector;
|
|
1652
2223
|
}
|
|
1653
2224
|
if (elements[0].matches('form')) {
|
|
1654
|
-
return
|
|
2225
|
+
return validate(elements[0])?.form() ?? false;
|
|
1655
2226
|
}
|
|
1656
2227
|
const errorList = [];
|
|
1657
2228
|
let valid = true;
|
|
@@ -1668,6 +2239,37 @@
|
|
|
1668
2239
|
validator.errorList = errorList;
|
|
1669
2240
|
return valid;
|
|
1670
2241
|
}
|
|
2242
|
+
/**
|
|
2243
|
+
* Like `dv.valid`, but waits for async validations (such as `remote`) to
|
|
2244
|
+
* finish, and resolves with the final result.
|
|
2245
|
+
*
|
|
2246
|
+
* @param selector - Element(s) to validate:
|
|
2247
|
+
* CSS selector, element, or array of elements
|
|
2248
|
+
* @returns Promise of true if all elements are valid, false otherwise
|
|
2249
|
+
* @example
|
|
2250
|
+
* if (await dv.validAsync('#signupForm')) {
|
|
2251
|
+
* await saveDraft();
|
|
2252
|
+
* }
|
|
2253
|
+
*/
|
|
2254
|
+
async function validAsync(selector) {
|
|
2255
|
+
let result = valid(selector);
|
|
2256
|
+
const first = typeof selector === 'string'
|
|
2257
|
+
? document.querySelector(selector)
|
|
2258
|
+
: Array.isArray(selector)
|
|
2259
|
+
? selector[0]
|
|
2260
|
+
: selector;
|
|
2261
|
+
const form = first instanceof HTMLFormElement
|
|
2262
|
+
? first
|
|
2263
|
+
: (first?.form ?? null);
|
|
2264
|
+
const validator = form ? validatorStore.get(form) : undefined;
|
|
2265
|
+
// wait for the answers, then validate again with them; repeat if a field
|
|
2266
|
+
// changed meanwhile and started a new request
|
|
2267
|
+
while (validator?.pendingRequest) {
|
|
2268
|
+
await validator.settled();
|
|
2269
|
+
result = validator.withCachedResults(() => valid(selector));
|
|
2270
|
+
}
|
|
2271
|
+
return result;
|
|
2272
|
+
}
|
|
1671
2273
|
/**
|
|
1672
2274
|
* Gets the validation rules for an element.
|
|
1673
2275
|
*
|
|
@@ -1689,13 +2291,15 @@
|
|
|
1689
2291
|
return getRules(element, validator.rules);
|
|
1690
2292
|
}
|
|
1691
2293
|
/**
|
|
1692
|
-
* Adds a custom validation method
|
|
2294
|
+
* Adds a custom validation method, or replaces an existing one (as jQuery's
|
|
2295
|
+
* `$.validator.addMethod` does, e.g. `number` for a culture).
|
|
1693
2296
|
* The method can be used in validation rules by its name.
|
|
1694
2297
|
*
|
|
1695
2298
|
* @param name - Name of the validation method
|
|
1696
2299
|
* @param method - Validation function that returns true if valid
|
|
1697
|
-
* @param message - Default error message for this validation method
|
|
1698
|
-
*
|
|
2300
|
+
* @param message - Default error message for this validation method; when
|
|
2301
|
+
* omitted, an existing method keeps its message
|
|
2302
|
+
* @param addAsClassRule - Whether to allow using this method as a CSS class (default: false). Set to true for methods that take no param.
|
|
1699
2303
|
* @example
|
|
1700
2304
|
* // Add a custom phone validation method
|
|
1701
2305
|
* dv.addMethod('phone', function(blank, value, element) {
|
|
@@ -1715,12 +2319,10 @@
|
|
|
1715
2319
|
console.warn('Invalid method name: must be non-empty and only alphanumeric characters.');
|
|
1716
2320
|
return;
|
|
1717
2321
|
}
|
|
1718
|
-
if (store$1.get(name)) {
|
|
1719
|
-
console.warn(`Invalid method name: ${name} already exists.`);
|
|
1720
|
-
return;
|
|
1721
|
-
}
|
|
1722
2322
|
store$1.set(name, method);
|
|
1723
|
-
|
|
2323
|
+
if (message !== undefined) {
|
|
2324
|
+
store.set(name, message);
|
|
2325
|
+
}
|
|
1724
2326
|
if (addAsClassRule) {
|
|
1725
2327
|
addClassRule(name);
|
|
1726
2328
|
}
|
|
@@ -1756,6 +2358,7 @@
|
|
|
1756
2358
|
var index = {
|
|
1757
2359
|
validate,
|
|
1758
2360
|
valid,
|
|
2361
|
+
validAsync,
|
|
1759
2362
|
rules,
|
|
1760
2363
|
addMethod,
|
|
1761
2364
|
localize,
|
|
@@ -1770,6 +2373,7 @@
|
|
|
1770
2373
|
exports.methods = store$1;
|
|
1771
2374
|
exports.rules = rules;
|
|
1772
2375
|
exports.valid = valid;
|
|
2376
|
+
exports.validAsync = validAsync;
|
|
1773
2377
|
exports.validate = validate;
|
|
1774
2378
|
|
|
1775
2379
|
Object.defineProperty(exports, '__esModule', { value: true });
|