dino-validation 0.3.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 +123 -1687
- package/THIRD_PARTY_NOTICES.md +69 -0
- package/dist/dv.esm.js +770 -105
- package/dist/dv.esm.js.map +1 -1
- package/dist/dv.js +770 -104
- 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 +6 -1
- 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 -3
- 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 +34 -8
- 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 +27 -8
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);
|
|
@@ -259,20 +379,23 @@
|
|
|
259
379
|
email,
|
|
260
380
|
url,
|
|
261
381
|
date,
|
|
262
|
-
dateISO,
|
|
263
382
|
number,
|
|
264
383
|
digits,
|
|
265
|
-
equalTo,
|
|
384
|
+
equalto: equalTo,
|
|
266
385
|
regex,
|
|
267
386
|
nonalphamin: nonAlphaMin,
|
|
268
387
|
creditcard: creditCard,
|
|
388
|
+
extension,
|
|
389
|
+
remote,
|
|
269
390
|
});
|
|
270
391
|
/**
|
|
271
|
-
* Validates that the field has a value (is not blank).
|
|
272
|
-
*
|
|
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
|
|
273
396
|
*/
|
|
274
|
-
function required({ blank }) {
|
|
275
|
-
return !blank;
|
|
397
|
+
function required({ blank, param, element }) {
|
|
398
|
+
return !blank || !depend(param, element);
|
|
276
399
|
}
|
|
277
400
|
/**
|
|
278
401
|
* Validates that the field value has at least the minimum length.
|
|
@@ -345,25 +468,13 @@
|
|
|
345
468
|
const re = /^(?:(?:https?|ftp):)?\/\/(?:(?:[^\]\[?\/<~#`!@$^&*()+=}|:";',>{ ]|%[0-9A-Fa-f]{2})+(?::(?:[^\]\[?\/<~#`!@$^&*()+=}|:";',>{ ]|%[0-9A-Fa-f]{2})*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4])|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+[a-z\u00a1-\uffff]{2,}\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$/i;
|
|
346
469
|
return re.test(value);
|
|
347
470
|
}
|
|
348
|
-
/**
|
|
349
|
-
* Validates that the field value is a valid date string
|
|
350
|
-
* @param blank - whether the field is blank
|
|
351
|
-
* @param value - field value
|
|
352
|
-
* @return true if blank or valid date string
|
|
353
|
-
*/
|
|
354
|
-
function date({ blank, value }) {
|
|
355
|
-
if (blank) {
|
|
356
|
-
return true;
|
|
357
|
-
}
|
|
358
|
-
return !/Invalid|NaN/.test(new Date(value).toString());
|
|
359
|
-
}
|
|
360
471
|
/**
|
|
361
472
|
* Validates that the field value is a valid ISO date format (YYYY-MM-DD or YYYY/MM/DD).
|
|
362
473
|
* @param blank - whether the field is blank
|
|
363
474
|
* @param value - field value
|
|
364
475
|
* @return true if blank or valid ISO date format
|
|
365
476
|
*/
|
|
366
|
-
function
|
|
477
|
+
function date({ blank, value }) {
|
|
367
478
|
if (blank) {
|
|
368
479
|
return true;
|
|
369
480
|
}
|
|
@@ -400,8 +511,8 @@
|
|
|
400
511
|
* @return true if values match
|
|
401
512
|
*/
|
|
402
513
|
function equalTo({ value, param }) {
|
|
403
|
-
const target = document.querySelector(param);
|
|
404
|
-
return value === target.value;
|
|
514
|
+
const target = param instanceof Element ? param : document.querySelector(param);
|
|
515
|
+
return !!target && value === target.value;
|
|
405
516
|
}
|
|
406
517
|
/**
|
|
407
518
|
* Validates that the field value matches a regular expression.
|
|
@@ -435,6 +546,10 @@
|
|
|
435
546
|
if (blank) {
|
|
436
547
|
return true;
|
|
437
548
|
}
|
|
549
|
+
// only digits, spaces and dashes are allowed
|
|
550
|
+
if (/[^0-9 \-]+/.test(value)) {
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
438
553
|
const strippedValue = value.replace(/\D/g, '');
|
|
439
554
|
let nCheck = 0;
|
|
440
555
|
let bEven = false;
|
|
@@ -456,6 +571,77 @@
|
|
|
456
571
|
}
|
|
457
572
|
return nCheck % 10 === 0;
|
|
458
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
|
+
}
|
|
459
645
|
|
|
460
646
|
/**
|
|
461
647
|
* Gets all validation rules for an element from multiple sources.
|
|
@@ -469,12 +655,23 @@
|
|
|
469
655
|
if (element == null || element.form == null) {
|
|
470
656
|
return {};
|
|
471
657
|
}
|
|
472
|
-
let data = normalizeRules(
|
|
658
|
+
let data = normalizeRules({
|
|
659
|
+
...classRules(element),
|
|
660
|
+
...attributeRules(element),
|
|
661
|
+
...dataRules(element),
|
|
662
|
+
...staticRules(element, settings),
|
|
663
|
+
}, element);
|
|
473
664
|
// Make sure required is at front
|
|
474
|
-
if (data
|
|
665
|
+
if ('required' in data) {
|
|
475
666
|
const param = data.required;
|
|
476
667
|
delete data.required;
|
|
477
|
-
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;
|
|
478
675
|
}
|
|
479
676
|
return data;
|
|
480
677
|
}
|
|
@@ -487,7 +684,6 @@
|
|
|
487
684
|
email: { email: true },
|
|
488
685
|
url: { url: true },
|
|
489
686
|
date: { date: true },
|
|
490
|
-
dateISO: { dateISO: true },
|
|
491
687
|
number: { number: true },
|
|
492
688
|
digits: { digits: true },
|
|
493
689
|
creditcard: { creditcard: true },
|
|
@@ -501,19 +697,37 @@
|
|
|
501
697
|
}
|
|
502
698
|
/**
|
|
503
699
|
* Normalizes and processes validation rules for an element.
|
|
504
|
-
* Removes false rules,
|
|
700
|
+
* Removes false rules, applies conditional rules (`{ param, depends }`),
|
|
701
|
+
* evaluates function parameters, and converts string/number values.
|
|
505
702
|
* @param rules - raw validation rules object
|
|
703
|
+
* @param element - the element the rules are for
|
|
506
704
|
* @return normalized rules object
|
|
507
705
|
*/
|
|
508
|
-
function normalizeRules(rules) {
|
|
706
|
+
function normalizeRules(rules, element) {
|
|
509
707
|
Object.entries(rules).forEach(([key, value]) => {
|
|
510
|
-
// Ignore rule when param is explicitly false, eg. required:false
|
|
511
|
-
|
|
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) {
|
|
512
711
|
delete rules[key];
|
|
513
712
|
return;
|
|
514
713
|
}
|
|
515
|
-
|
|
516
|
-
|
|
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);
|
|
517
731
|
}
|
|
518
732
|
});
|
|
519
733
|
// Clean number parameters
|
|
@@ -547,9 +761,13 @@
|
|
|
547
761
|
let rules = {};
|
|
548
762
|
const classes = element.getAttribute('class');
|
|
549
763
|
if (classes) {
|
|
550
|
-
classes
|
|
764
|
+
classes
|
|
765
|
+
.trim()
|
|
766
|
+
.split(/\s/)
|
|
767
|
+
.map((className) => className.toLowerCase())
|
|
768
|
+
.forEach((className) => {
|
|
551
769
|
if (className in classRuleSettings) {
|
|
552
|
-
rules =
|
|
770
|
+
rules = { ...rules, ...classRuleSettings[className] };
|
|
553
771
|
}
|
|
554
772
|
});
|
|
555
773
|
}
|
|
@@ -590,14 +808,13 @@
|
|
|
590
808
|
* @return validation rules derived from data attributes
|
|
591
809
|
*/
|
|
592
810
|
function dataRules(element) {
|
|
593
|
-
var _a;
|
|
594
811
|
const rules = {};
|
|
595
812
|
const type = element.type;
|
|
596
813
|
for (const method of store$1.keys()) {
|
|
597
814
|
const datasetKey = 'rule' +
|
|
598
815
|
method.charAt(0).toUpperCase() +
|
|
599
816
|
method.substring(1).toLowerCase();
|
|
600
|
-
let value =
|
|
817
|
+
let value = element.dataset[datasetKey] ?? null;
|
|
601
818
|
// Cast empty attributes like `data-rule-required` to `true`
|
|
602
819
|
if (value === '' || value === 'true') {
|
|
603
820
|
value = true;
|
|
@@ -643,7 +860,7 @@
|
|
|
643
860
|
else if (type === method && type !== 'range') {
|
|
644
861
|
// Exception: the jquery validate 'range' method
|
|
645
862
|
// does not test for the html5 'range' type
|
|
646
|
-
rules[
|
|
863
|
+
rules[method] = true;
|
|
647
864
|
}
|
|
648
865
|
}
|
|
649
866
|
/**
|
|
@@ -654,10 +871,14 @@
|
|
|
654
871
|
*/
|
|
655
872
|
function normalizeRule(data) {
|
|
656
873
|
if (typeof data !== 'string') {
|
|
657
|
-
return data;
|
|
874
|
+
return Object.fromEntries(Object.entries(data).map(([key, value]) => [key.toLowerCase(), value]));
|
|
658
875
|
}
|
|
659
876
|
const transformed = {};
|
|
660
|
-
data
|
|
877
|
+
data
|
|
878
|
+
.trim()
|
|
879
|
+
.split(/\s/)
|
|
880
|
+
.map((token) => token.toLowerCase())
|
|
881
|
+
.forEach((token) => {
|
|
661
882
|
transformed[token] = true;
|
|
662
883
|
});
|
|
663
884
|
return transformed;
|
|
@@ -671,10 +892,9 @@
|
|
|
671
892
|
email: 'Please enter a valid email address.',
|
|
672
893
|
url: 'Please enter a valid URL.',
|
|
673
894
|
date: 'Please enter a valid date.',
|
|
674
|
-
dateISO: 'Please enter a valid date (ISO).',
|
|
675
895
|
number: 'Please enter a valid number.',
|
|
676
896
|
digits: 'Please enter only digits.',
|
|
677
|
-
|
|
897
|
+
equalto: 'Please enter the same value again.',
|
|
678
898
|
maxlength: 'Please enter no more than {0} characters.',
|
|
679
899
|
minlength: 'Please enter at least {0} characters.',
|
|
680
900
|
rangelength: 'Please enter a value between {0} and {1} characters long.',
|
|
@@ -684,6 +904,8 @@
|
|
|
684
904
|
regex: 'Please enter a value that matches the pattern {0}.',
|
|
685
905
|
nonalphamin: 'Please enter at least {0} non-alphabetic characters.',
|
|
686
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.',
|
|
687
909
|
});
|
|
688
910
|
/**
|
|
689
911
|
* Gets the appropriate error message for a validation rule failure.
|
|
@@ -695,13 +917,17 @@
|
|
|
695
917
|
* @return formatted error message
|
|
696
918
|
*/
|
|
697
919
|
function getMessage(element, rule, ignoreTitle, customMessage) {
|
|
698
|
-
|
|
920
|
+
const message = [
|
|
699
921
|
customMessage,
|
|
700
922
|
customDataMessage(element, rule.method),
|
|
701
923
|
(!ignoreTitle && element.title) || undefined,
|
|
702
924
|
store.get(rule.method),
|
|
703
925
|
`<strong>Warning: No message defined for ${element.name}</strong>`,
|
|
704
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
|
+
}
|
|
705
931
|
const regex = /\$?\{(\d+)}/g;
|
|
706
932
|
if (/\$?\{(\d+)}/g.test(message)) {
|
|
707
933
|
return format(message.replace(regex, '{$1}'), rule.parameters);
|
|
@@ -719,7 +945,7 @@
|
|
|
719
945
|
*/
|
|
720
946
|
function customDataMessage(element, method) {
|
|
721
947
|
const dataSetKey = 'msg' + method.charAt(0).toUpperCase() + method.substring(1).toLowerCase();
|
|
722
|
-
if (!
|
|
948
|
+
if (!element?.dataset) {
|
|
723
949
|
return;
|
|
724
950
|
}
|
|
725
951
|
return element.dataset[dataSetKey] || element.dataset['msg'];
|
|
@@ -733,6 +959,8 @@
|
|
|
733
959
|
const EVENT_TARGETS = {
|
|
734
960
|
FOCUS_TARGETS: [
|
|
735
961
|
"[type='text']",
|
|
962
|
+
// an <input> without a type is a text input
|
|
963
|
+
'input:not([type])',
|
|
736
964
|
"[type='password']",
|
|
737
965
|
"[type='file']",
|
|
738
966
|
'select',
|
|
@@ -762,18 +990,24 @@
|
|
|
762
990
|
* Handles attaching and removing event listeners with proper delegation logic.
|
|
763
991
|
*/
|
|
764
992
|
class FormEventManager {
|
|
765
|
-
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) {
|
|
766
996
|
this.form = form;
|
|
767
997
|
this.settings = settings;
|
|
768
998
|
this.validateCallback = validateCallback;
|
|
769
999
|
this.focusInvalidCallback = focusInvalidCallback;
|
|
770
1000
|
this.shouldIgnoreElement = shouldIgnoreElement;
|
|
1001
|
+
this.holdSubmitCallback = holdSubmitCallback;
|
|
771
1002
|
this.boundEventHandlers = {
|
|
772
1003
|
onFocusIn: null,
|
|
773
1004
|
onFocusOut: null,
|
|
774
1005
|
onKeyUp: null,
|
|
775
1006
|
onClick: null,
|
|
1007
|
+
onSubmit: null,
|
|
1008
|
+
onInvalidForm: null,
|
|
776
1009
|
};
|
|
1010
|
+
this.submitButton = null;
|
|
777
1011
|
}
|
|
778
1012
|
/**
|
|
779
1013
|
* Attaches all validation event listeners to the form
|
|
@@ -788,16 +1022,18 @@
|
|
|
788
1022
|
this.form.addEventListener('keyup', this.boundEventHandlers.onKeyUp);
|
|
789
1023
|
this.form.addEventListener('click', this.boundEventHandlers.onClick);
|
|
790
1024
|
if (this.settings.onsubmit) {
|
|
791
|
-
this.
|
|
1025
|
+
this.boundEventHandlers.onSubmit = (e) => this.handleSubmitForm(e);
|
|
1026
|
+
this.form.addEventListener('submit', this.boundEventHandlers.onSubmit);
|
|
792
1027
|
}
|
|
1028
|
+
// the validator wraps it to pass itself as the second argument
|
|
793
1029
|
const invalidHandler = this.settings.invalidHandler;
|
|
794
1030
|
if (invalidHandler) {
|
|
795
|
-
this.
|
|
796
|
-
if (
|
|
797
|
-
|
|
1031
|
+
this.boundEventHandlers.onInvalidForm = (e) => {
|
|
1032
|
+
if (this.isCustomEvent(e)) {
|
|
1033
|
+
invalidHandler(e);
|
|
798
1034
|
}
|
|
799
|
-
|
|
800
|
-
|
|
1035
|
+
};
|
|
1036
|
+
this.form.addEventListener('invalid-form', this.boundEventHandlers.onInvalidForm);
|
|
801
1037
|
}
|
|
802
1038
|
}
|
|
803
1039
|
triggerInvalidForm(errorList) {
|
|
@@ -814,11 +1050,54 @@
|
|
|
814
1050
|
if (this.settings.debug) {
|
|
815
1051
|
e.preventDefault();
|
|
816
1052
|
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
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
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
let hidden = null;
|
|
1082
|
+
// Insert a hidden input as a replacement for the missing submit button
|
|
1083
|
+
// if a submitHandler is passed as option
|
|
1084
|
+
if (this.submitButton?.name && this.settings.submitHandler) {
|
|
1085
|
+
hidden = document.createElement('input');
|
|
1086
|
+
hidden.setAttribute('type', 'hidden');
|
|
1087
|
+
hidden.setAttribute('name', this.submitButton.name);
|
|
1088
|
+
hidden.setAttribute('value', this.submitButton.value);
|
|
1089
|
+
this.form.appendChild(hidden);
|
|
1090
|
+
}
|
|
1091
|
+
if (this.settings.submitHandler && !this.settings.debug) {
|
|
1092
|
+
const result = this.settings.submitHandler(this.form, e);
|
|
1093
|
+
if (hidden) {
|
|
1094
|
+
// And clean up afterwards
|
|
1095
|
+
hidden.remove();
|
|
1096
|
+
}
|
|
1097
|
+
if (!result) {
|
|
1098
|
+
e.preventDefault();
|
|
1099
|
+
e.stopPropagation();
|
|
1100
|
+
}
|
|
822
1101
|
}
|
|
823
1102
|
}
|
|
824
1103
|
/**
|
|
@@ -837,11 +1116,19 @@
|
|
|
837
1116
|
if (this.boundEventHandlers.onClick) {
|
|
838
1117
|
this.form.removeEventListener('click', this.boundEventHandlers.onClick);
|
|
839
1118
|
}
|
|
1119
|
+
if (this.boundEventHandlers.onSubmit) {
|
|
1120
|
+
this.form.removeEventListener('submit', this.boundEventHandlers.onSubmit);
|
|
1121
|
+
}
|
|
1122
|
+
if (this.boundEventHandlers.onInvalidForm) {
|
|
1123
|
+
this.form.removeEventListener('invalid-form', this.boundEventHandlers.onInvalidForm);
|
|
1124
|
+
}
|
|
840
1125
|
// Clear references
|
|
841
1126
|
this.boundEventHandlers.onFocusIn = null;
|
|
842
1127
|
this.boundEventHandlers.onFocusOut = null;
|
|
843
1128
|
this.boundEventHandlers.onKeyUp = null;
|
|
844
1129
|
this.boundEventHandlers.onClick = null;
|
|
1130
|
+
this.boundEventHandlers.onSubmit = null;
|
|
1131
|
+
this.boundEventHandlers.onInvalidForm = null;
|
|
845
1132
|
}
|
|
846
1133
|
/**
|
|
847
1134
|
* Creates a delegated event handler for the specified targets and handler
|
|
@@ -889,6 +1176,7 @@
|
|
|
889
1176
|
ignore: ':hidden',
|
|
890
1177
|
errorClass: 'error',
|
|
891
1178
|
validClass: 'valid',
|
|
1179
|
+
pendingClass: 'pending',
|
|
892
1180
|
errorElement: 'label',
|
|
893
1181
|
wrapper: null,
|
|
894
1182
|
errorLabelContainer: null,
|
|
@@ -910,11 +1198,12 @@
|
|
|
910
1198
|
onsubmit: true,
|
|
911
1199
|
debug: false,
|
|
912
1200
|
invalidHandler: null,
|
|
1201
|
+
submitHandler: null,
|
|
913
1202
|
};
|
|
914
1203
|
function onFocusIn(element) {
|
|
915
1204
|
this.lastActive = element;
|
|
916
1205
|
if (this.settings.focusCleanup) {
|
|
917
|
-
if (
|
|
1206
|
+
if (this.settings.unhighlight) {
|
|
918
1207
|
this.settings.unhighlight(element, this.errorClasses, this.validClasses);
|
|
919
1208
|
}
|
|
920
1209
|
this.hideErrors(element);
|
|
@@ -993,6 +1282,40 @@
|
|
|
993
1282
|
});
|
|
994
1283
|
}
|
|
995
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
|
+
]);
|
|
996
1319
|
class Validator {
|
|
997
1320
|
/**
|
|
998
1321
|
* Initializes a new validator instance for the given form with optional configuration settings.
|
|
@@ -1001,6 +1324,7 @@
|
|
|
1001
1324
|
*/
|
|
1002
1325
|
constructor(form, options) {
|
|
1003
1326
|
this.submitted = {};
|
|
1327
|
+
/** fields validated so far: their message, or false when valid */
|
|
1004
1328
|
this.invalid = {};
|
|
1005
1329
|
this.successList = [];
|
|
1006
1330
|
this.errorList = [];
|
|
@@ -1013,9 +1337,27 @@
|
|
|
1013
1337
|
this.lastActive = null;
|
|
1014
1338
|
/** normalized rules */
|
|
1015
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 = [];
|
|
1016
1358
|
this.settings = ValidatorDefaults;
|
|
1017
1359
|
this.currentForm = form;
|
|
1018
|
-
this.settings =
|
|
1360
|
+
this.settings = { ...this.settings, ...options };
|
|
1019
1361
|
this.normalizeSettings();
|
|
1020
1362
|
this.init();
|
|
1021
1363
|
}
|
|
@@ -1033,34 +1375,103 @@
|
|
|
1033
1375
|
'highlight',
|
|
1034
1376
|
'unhighlight',
|
|
1035
1377
|
'errorPlacement',
|
|
1036
|
-
'
|
|
1378
|
+
'submitHandler',
|
|
1037
1379
|
].forEach((key) => {
|
|
1038
1380
|
const setting = this.settings[key];
|
|
1039
1381
|
if (setting && typeof setting === 'function') {
|
|
1040
1382
|
this.settings[key] = setting.bind(this);
|
|
1041
1383
|
}
|
|
1042
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 };
|
|
1393
|
+
// transform method names to lowercase in messages
|
|
1394
|
+
if (this.settings.messages) {
|
|
1395
|
+
Object.keys(this.settings.messages).forEach((elementName) => {
|
|
1396
|
+
const messages = this.settings.messages[elementName];
|
|
1397
|
+
// Only transform if it's an object (not a string)
|
|
1398
|
+
if (typeof messages === 'object' && messages !== null) {
|
|
1399
|
+
const normalizedMessages = {};
|
|
1400
|
+
Object.keys(messages).forEach((methodName) => {
|
|
1401
|
+
normalizedMessages[methodName.toLowerCase()] =
|
|
1402
|
+
messages[methodName];
|
|
1403
|
+
});
|
|
1404
|
+
this.settings.messages[elementName] = normalizedMessages;
|
|
1405
|
+
}
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1043
1408
|
}
|
|
1044
1409
|
/**
|
|
1045
1410
|
* Sets up DOM references and attaches event handlers to the form.
|
|
1046
1411
|
*/
|
|
1047
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
|
+
}
|
|
1048
1419
|
if (this.settings.errorLabelContainer) {
|
|
1049
1420
|
this.labelContainer = document.querySelector(this.settings.errorLabelContainer);
|
|
1050
1421
|
}
|
|
1051
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
|
|
1052
1425
|
const containers = [this.labelContainer];
|
|
1053
1426
|
if (this.settings.errorContainer) {
|
|
1054
|
-
containers.push(document.
|
|
1427
|
+
containers.push(...document.querySelectorAll(this.settings.errorContainer));
|
|
1055
1428
|
}
|
|
1056
1429
|
this.containers = containers.filter(Boolean);
|
|
1057
1430
|
this.rules = {};
|
|
1058
1431
|
Object.entries(this.settings.rules).forEach(([key, value]) => {
|
|
1059
1432
|
this.rules[key] = normalizeRule(value);
|
|
1060
1433
|
});
|
|
1061
|
-
|
|
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));
|
|
1062
1443
|
this.eventManager.attachEventHandlers();
|
|
1063
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
|
+
}
|
|
1064
1475
|
/**
|
|
1065
1476
|
* Resets the validator state.
|
|
1066
1477
|
*/
|
|
@@ -1077,17 +1488,19 @@
|
|
|
1077
1488
|
* @return true if the form is valid, false otherwise
|
|
1078
1489
|
*/
|
|
1079
1490
|
form() {
|
|
1491
|
+
this.retryFailedRequests();
|
|
1080
1492
|
this.prepareForm();
|
|
1081
1493
|
this.currentElements = this.elements();
|
|
1082
1494
|
for (const element of this.currentElements) {
|
|
1083
1495
|
this.check(element);
|
|
1084
1496
|
}
|
|
1085
|
-
this.submitted =
|
|
1086
|
-
this.invalid =
|
|
1497
|
+
this.submitted = { ...this.submitted, ...this.errorMap };
|
|
1498
|
+
this.invalid = { ...this.errorMap };
|
|
1087
1499
|
if (!this.valid()) {
|
|
1088
1500
|
this.eventManager.triggerInvalidForm(this.errorList);
|
|
1089
1501
|
}
|
|
1090
1502
|
this.showErrors();
|
|
1503
|
+
this.notifySettled();
|
|
1091
1504
|
return this.valid();
|
|
1092
1505
|
}
|
|
1093
1506
|
/**
|
|
@@ -1096,6 +1509,7 @@
|
|
|
1096
1509
|
* @return true if the element is valid, false otherwise
|
|
1097
1510
|
*/
|
|
1098
1511
|
element(element) {
|
|
1512
|
+
this.retryFailedRequests();
|
|
1099
1513
|
const target = this.validationTargetFor(element);
|
|
1100
1514
|
if (target === undefined) {
|
|
1101
1515
|
delete this.invalid[element.name];
|
|
@@ -1104,18 +1518,16 @@
|
|
|
1104
1518
|
this.prepareElement(target);
|
|
1105
1519
|
this.currentElements = [target];
|
|
1106
1520
|
const result = this.check(target);
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
else {
|
|
1111
|
-
this.invalid[target.name] = 'Invalid';
|
|
1112
|
-
}
|
|
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';
|
|
1113
1524
|
if (!this.numberOfInvalids()) {
|
|
1114
1525
|
this.toHide.push(...this.containers);
|
|
1115
1526
|
}
|
|
1116
1527
|
this.showErrors();
|
|
1117
1528
|
// add aria-invalid status for screen readers
|
|
1118
1529
|
element.setAttribute('aria-invalid', (!result).toString());
|
|
1530
|
+
this.notifySettled();
|
|
1119
1531
|
return result;
|
|
1120
1532
|
}
|
|
1121
1533
|
prepareElement(element) {
|
|
@@ -1143,9 +1555,11 @@
|
|
|
1143
1555
|
elements() {
|
|
1144
1556
|
const rulesCache = {};
|
|
1145
1557
|
const selector = 'input, select, textarea';
|
|
1146
|
-
|
|
1558
|
+
// :disabled also matches fields inside <fieldset disabled>
|
|
1559
|
+
const notSelector = 'input[type="submit"], [type="reset"], [type="image"], :disabled';
|
|
1147
1560
|
const elements = Array.from(this.currentForm.querySelectorAll(selector)).filter((el) => !el.matches(notSelector) && !this.shouldIgnore(el));
|
|
1148
|
-
|
|
1561
|
+
// getAttribute: a field named "id" shadows form.id
|
|
1562
|
+
const formId = this.currentForm.getAttribute('id');
|
|
1149
1563
|
if (formId) {
|
|
1150
1564
|
const escapedId = escapeCssMeta(formId);
|
|
1151
1565
|
const outsideElements = Array.from(document.querySelectorAll(selector)).filter((el) => el.matches(`[form="${escapedId}"]`) &&
|
|
@@ -1174,7 +1588,8 @@
|
|
|
1174
1588
|
*/
|
|
1175
1589
|
validElements() {
|
|
1176
1590
|
const invalid = this.invalidElements();
|
|
1177
|
-
|
|
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));
|
|
1178
1593
|
}
|
|
1179
1594
|
/**
|
|
1180
1595
|
* Returns an array of currently invalid elements from the last validation.
|
|
@@ -1189,14 +1604,24 @@
|
|
|
1189
1604
|
* @return true if the element is valid, false otherwise
|
|
1190
1605
|
*/
|
|
1191
1606
|
check(element) {
|
|
1192
|
-
var _a;
|
|
1193
1607
|
element = this.validationTargetFor(element);
|
|
1194
1608
|
const rules = getRules(element, this.rules);
|
|
1195
1609
|
const elValue = elementValue(element, this.currentForm);
|
|
1196
|
-
const value = (
|
|
1610
|
+
const value = (Array.isArray(elValue) ? elValue[0] : elValue) ?? '';
|
|
1197
1611
|
const values = Array.isArray(elValue) ? elValue : [elValue];
|
|
1198
1612
|
const length = getValueLength(element, this.currentForm);
|
|
1199
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;
|
|
1200
1625
|
for (const method in rules) {
|
|
1201
1626
|
const rule = { method, parameters: rules[method] };
|
|
1202
1627
|
const methodFunc = store$1.get(method);
|
|
@@ -1204,7 +1629,25 @@
|
|
|
1204
1629
|
console.warn(`Validation method "${method}" not found. Skipping.`);
|
|
1205
1630
|
continue;
|
|
1206
1631
|
}
|
|
1207
|
-
|
|
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();
|
|
1208
1651
|
const result = methodFunc({
|
|
1209
1652
|
blank,
|
|
1210
1653
|
value: value,
|
|
@@ -1212,21 +1655,189 @@
|
|
|
1212
1655
|
length,
|
|
1213
1656
|
element,
|
|
1214
1657
|
param: rule.parameters,
|
|
1658
|
+
signal: controller.signal,
|
|
1215
1659
|
});
|
|
1216
|
-
if (!result) {
|
|
1217
|
-
|
|
1218
|
-
|
|
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;
|
|
1219
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));
|
|
1220
1675
|
}
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
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;
|
|
1224
1681
|
}
|
|
1225
|
-
if (objectLength(rules)) {
|
|
1682
|
+
if (!neutral && objectLength(rules)) {
|
|
1226
1683
|
this.successList.push(element);
|
|
1227
1684
|
}
|
|
1228
1685
|
return true;
|
|
1229
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
|
+
}
|
|
1230
1841
|
/**
|
|
1231
1842
|
* Returns all error label elements currently in the DOM.
|
|
1232
1843
|
* @return array of error label elements
|
|
@@ -1238,7 +1849,7 @@
|
|
|
1238
1849
|
showErrors(errors) {
|
|
1239
1850
|
if (errors) {
|
|
1240
1851
|
// Add items to error list and map
|
|
1241
|
-
this.errorMap =
|
|
1852
|
+
this.errorMap = { ...this.errorMap, ...errors };
|
|
1242
1853
|
this.errorList = Object.entries(this.errorMap).map(([name, message]) => ({
|
|
1243
1854
|
message,
|
|
1244
1855
|
method: message,
|
|
@@ -1276,8 +1887,7 @@
|
|
|
1276
1887
|
this.hideErrors();
|
|
1277
1888
|
this.addWrapper(...this.toShow).forEach(showElement);
|
|
1278
1889
|
}
|
|
1279
|
-
formatAndAdd(element, rule) {
|
|
1280
|
-
const message = this.getMessage(element, rule);
|
|
1890
|
+
formatAndAdd(element, rule, message = this.getMessage(element, rule)) {
|
|
1281
1891
|
this.errorList.push({ message, element, method: rule.method });
|
|
1282
1892
|
this.errorMap[element.name] = message;
|
|
1283
1893
|
this.submitted[element.name] = message;
|
|
@@ -1311,7 +1921,6 @@
|
|
|
1311
1921
|
return result;
|
|
1312
1922
|
}
|
|
1313
1923
|
showLabel(element, message) {
|
|
1314
|
-
var _a;
|
|
1315
1924
|
let errors = this.errorsFor(element);
|
|
1316
1925
|
const elementID = idOrName(element);
|
|
1317
1926
|
const describedBy = element.getAttribute('aria-describedby');
|
|
@@ -1322,11 +1931,10 @@
|
|
|
1322
1931
|
.map((e) => e.closest(labelSelector))
|
|
1323
1932
|
.every((e) => e === null);
|
|
1324
1933
|
const noDescriberAssociation = () => {
|
|
1325
|
-
var _a, _b;
|
|
1326
1934
|
if (describedBy === null) {
|
|
1327
1935
|
return true;
|
|
1328
1936
|
}
|
|
1329
|
-
const firstErrorId =
|
|
1937
|
+
const firstErrorId = errors[0]?.getAttribute('id') ?? null;
|
|
1330
1938
|
if (firstErrorId === null) {
|
|
1331
1939
|
return true;
|
|
1332
1940
|
}
|
|
@@ -1392,7 +2000,7 @@
|
|
|
1392
2000
|
}
|
|
1393
2001
|
// If the element is not a child of an associated label, then it's necessary
|
|
1394
2002
|
// to explicitly apply aria-describedby
|
|
1395
|
-
else if (!
|
|
2003
|
+
else if (!newError.parentElement?.matches(`label[for='${escapeCssMeta(elementID)}']`)) {
|
|
1396
2004
|
this.addErrorAriaDescribedBy(element, newError.getAttribute('id'));
|
|
1397
2005
|
}
|
|
1398
2006
|
errors = [newError];
|
|
@@ -1436,10 +2044,27 @@
|
|
|
1436
2044
|
this.resetForm();
|
|
1437
2045
|
validatorStore.delete(this.currentForm);
|
|
1438
2046
|
this.eventManager.detachEventHandlers();
|
|
2047
|
+
if (this.addedNoValidate) {
|
|
2048
|
+
this.currentForm.removeAttribute('novalidate');
|
|
2049
|
+
}
|
|
1439
2050
|
}
|
|
1440
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();
|
|
1441
2065
|
this.invalid = {};
|
|
1442
2066
|
this.submitted = {};
|
|
2067
|
+
this.equalToChecked.clear();
|
|
1443
2068
|
this.prepareForm();
|
|
1444
2069
|
this.hideErrors();
|
|
1445
2070
|
this.elements().forEach((el) => el.removeAttribute('aria-invalid'));
|
|
@@ -1472,13 +2097,14 @@
|
|
|
1472
2097
|
* @return true if the element should be skipped, false otherwise
|
|
1473
2098
|
*/
|
|
1474
2099
|
shouldIgnore(element) {
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
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
|
+
];
|
|
1480
2106
|
}
|
|
1481
|
-
return
|
|
2107
|
+
return this.ignoreTest[1](element);
|
|
1482
2108
|
}
|
|
1483
2109
|
getMessage(element, rule) {
|
|
1484
2110
|
if (typeof rule === 'string') {
|
|
@@ -1585,7 +2211,6 @@
|
|
|
1585
2211
|
* }
|
|
1586
2212
|
*/
|
|
1587
2213
|
function valid(selector) {
|
|
1588
|
-
var _a, _b;
|
|
1589
2214
|
let elements = [];
|
|
1590
2215
|
if (typeof selector === 'string') {
|
|
1591
2216
|
elements = Array.from(document.querySelectorAll(selector));
|
|
@@ -1597,7 +2222,7 @@
|
|
|
1597
2222
|
elements = selector;
|
|
1598
2223
|
}
|
|
1599
2224
|
if (elements[0].matches('form')) {
|
|
1600
|
-
return
|
|
2225
|
+
return validate(elements[0])?.form() ?? false;
|
|
1601
2226
|
}
|
|
1602
2227
|
const errorList = [];
|
|
1603
2228
|
let valid = true;
|
|
@@ -1614,6 +2239,37 @@
|
|
|
1614
2239
|
validator.errorList = errorList;
|
|
1615
2240
|
return valid;
|
|
1616
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
|
+
}
|
|
1617
2273
|
/**
|
|
1618
2274
|
* Gets the validation rules for an element.
|
|
1619
2275
|
*
|
|
@@ -1635,12 +2291,15 @@
|
|
|
1635
2291
|
return getRules(element, validator.rules);
|
|
1636
2292
|
}
|
|
1637
2293
|
/**
|
|
1638
|
-
* 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).
|
|
1639
2296
|
* The method can be used in validation rules by its name.
|
|
1640
2297
|
*
|
|
1641
2298
|
* @param name - Name of the validation method
|
|
1642
2299
|
* @param method - Validation function that returns true if valid
|
|
1643
|
-
* @param message - Default error message for this validation method
|
|
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.
|
|
1644
2303
|
* @example
|
|
1645
2304
|
* // Add a custom phone validation method
|
|
1646
2305
|
* dv.addMethod('phone', function(blank, value, element) {
|
|
@@ -1654,12 +2313,17 @@
|
|
|
1654
2313
|
* }
|
|
1655
2314
|
* });
|
|
1656
2315
|
*/
|
|
1657
|
-
function addMethod(name, method, message) {
|
|
2316
|
+
function addMethod(name, method, message, addAsClassRule = false) {
|
|
2317
|
+
name = name.trim().toLowerCase();
|
|
2318
|
+
if (name === '' || !/^[a-zA-Z0-9]+$/.test(name)) {
|
|
2319
|
+
console.warn('Invalid method name: must be non-empty and only alphanumeric characters.');
|
|
2320
|
+
return;
|
|
2321
|
+
}
|
|
1658
2322
|
store$1.set(name, method);
|
|
1659
|
-
if (message) {
|
|
2323
|
+
if (message !== undefined) {
|
|
1660
2324
|
store.set(name, message);
|
|
1661
2325
|
}
|
|
1662
|
-
if (
|
|
2326
|
+
if (addAsClassRule) {
|
|
1663
2327
|
addClassRule(name);
|
|
1664
2328
|
}
|
|
1665
2329
|
}
|
|
@@ -1694,6 +2358,7 @@
|
|
|
1694
2358
|
var index = {
|
|
1695
2359
|
validate,
|
|
1696
2360
|
valid,
|
|
2361
|
+
validAsync,
|
|
1697
2362
|
rules,
|
|
1698
2363
|
addMethod,
|
|
1699
2364
|
localize,
|
|
@@ -1708,6 +2373,7 @@
|
|
|
1708
2373
|
exports.methods = store$1;
|
|
1709
2374
|
exports.rules = rules;
|
|
1710
2375
|
exports.valid = valid;
|
|
2376
|
+
exports.validAsync = validAsync;
|
|
1711
2377
|
exports.validate = validate;
|
|
1712
2378
|
|
|
1713
2379
|
Object.defineProperty(exports, '__esModule', { value: true });
|