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.esm.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)
|
|
@@ -41,8 +41,10 @@ function objectLength(obj) {
|
|
|
41
41
|
function findByName(form, name) {
|
|
42
42
|
const selector = `[name='${escapeCssMeta(name)}']`;
|
|
43
43
|
const elements = Array.from(form.querySelectorAll(selector));
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
// getAttribute: a field named "id" shadows form.id
|
|
45
|
+
const formId = form.getAttribute('id');
|
|
46
|
+
if (formId) {
|
|
47
|
+
const outsideElements = Array.from(document.querySelectorAll(selector)).filter((el) => el.matches(`[form="${escapeCssMeta(formId)}"]`));
|
|
46
48
|
elements.push(...outsideElements);
|
|
47
49
|
}
|
|
48
50
|
return elements;
|
|
@@ -140,7 +142,8 @@ function formatString(source, params) {
|
|
|
140
142
|
params = [params];
|
|
141
143
|
}
|
|
142
144
|
params.forEach((param, index) => {
|
|
143
|
-
|
|
145
|
+
// a replacement function: $&, $' and $` in the parameter stay as they are
|
|
146
|
+
source = source.replace(new RegExp(`\\{${index}\\}`, 'g'), () => String(param));
|
|
144
147
|
});
|
|
145
148
|
return source;
|
|
146
149
|
}
|
|
@@ -215,11 +218,128 @@ function hideElement(element) {
|
|
|
215
218
|
element.style.display = 'none';
|
|
216
219
|
}
|
|
217
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* Splits a selector list on its top-level commas, so `:not(a, b)` and
|
|
223
|
+
* `[title="a,b"]` stay whole.
|
|
224
|
+
* @param selector - selector list
|
|
225
|
+
* @return the selectors, trimmed, without empty ones
|
|
226
|
+
*/
|
|
227
|
+
function splitSelectorList(selector) {
|
|
228
|
+
const parts = [];
|
|
229
|
+
let depth = 0;
|
|
230
|
+
let quote = '';
|
|
231
|
+
let start = 0;
|
|
232
|
+
for (let i = 0; i < selector.length; i++) {
|
|
233
|
+
const c = selector[i];
|
|
234
|
+
if (quote) {
|
|
235
|
+
if (c === '\\')
|
|
236
|
+
i++;
|
|
237
|
+
else if (c === quote)
|
|
238
|
+
quote = '';
|
|
239
|
+
}
|
|
240
|
+
else if (c === '\\') {
|
|
241
|
+
i++;
|
|
242
|
+
}
|
|
243
|
+
else if (c === '"' || c === "'") {
|
|
244
|
+
quote = c;
|
|
245
|
+
}
|
|
246
|
+
else if (c === '(' || c === '[') {
|
|
247
|
+
depth++;
|
|
248
|
+
}
|
|
249
|
+
else if (c === ')' || c === ']') {
|
|
250
|
+
depth--;
|
|
251
|
+
}
|
|
252
|
+
else if (c === ',' && depth === 0) {
|
|
253
|
+
parts.push(selector.slice(start, i));
|
|
254
|
+
start = i + 1;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
parts.push(selector.slice(start));
|
|
258
|
+
return parts.map((part) => part.trim()).filter(Boolean);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Builds the test for the `ignore` setting: a selector list, or an array of
|
|
262
|
+
* them, where jQuery's `:hidden` means "not visible", also inside a compound
|
|
263
|
+
* selector such as `:hidden:not(.select2-hidden-accessible)`.
|
|
264
|
+
* An empty string or array ignores nothing. A selector the browser rejects
|
|
265
|
+
* ignores nothing (with a warning), so it can't turn validation off.
|
|
266
|
+
* @param ignore - the `ignore` setting
|
|
267
|
+
* @return function returning true for elements to ignore
|
|
268
|
+
*/
|
|
269
|
+
function ignoreMatcher(ignore) {
|
|
270
|
+
const selectors = (Array.isArray(ignore) ? ignore : [ignore || ''])
|
|
271
|
+
.flatMap((s) => splitSelectorList(String(s)))
|
|
272
|
+
.map((selector) => {
|
|
273
|
+
// :hidden alone, or as part of a compound selector: the rest of the
|
|
274
|
+
// selector must match too ("form :hidden" → "form *")
|
|
275
|
+
const hidden = /:hidden(?![\w-])/.test(selector);
|
|
276
|
+
const rest = selector
|
|
277
|
+
.replace(/(^|[\s>+~])?:hidden(?![\w-])/g, (_m, before) => before === undefined ? '' : `${before}*`)
|
|
278
|
+
.trim();
|
|
279
|
+
return { hidden, rest: rest === '*' ? '' : rest };
|
|
280
|
+
});
|
|
281
|
+
if (!selectors.length) {
|
|
282
|
+
return () => false;
|
|
283
|
+
}
|
|
284
|
+
const warned = new Set();
|
|
285
|
+
return (element) => selectors.some(({ hidden, rest }) => {
|
|
286
|
+
if (hidden && isVisible(element)) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
if (!rest) {
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
return element.matches(rest);
|
|
294
|
+
}
|
|
295
|
+
catch {
|
|
296
|
+
if (!warned.has(rest)) {
|
|
297
|
+
warned.add(rest);
|
|
298
|
+
console.warn(`dv: ignore selector "${rest}" is not valid, skipped`);
|
|
299
|
+
}
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Whether a conditional parameter holds for the element, as jQuery's
|
|
306
|
+
* `depend`: a boolean, a selector (true if it matches in the element's
|
|
307
|
+
* form, e.g. '#other:checked') or a function of the element.
|
|
308
|
+
* @param param - the parameter
|
|
309
|
+
* @param element - the element being validated
|
|
310
|
+
* @return true if the rule applies
|
|
311
|
+
*/
|
|
312
|
+
function depend(param, element) {
|
|
313
|
+
if (typeof param === 'boolean') {
|
|
314
|
+
return param;
|
|
315
|
+
}
|
|
316
|
+
if (typeof param === 'function') {
|
|
317
|
+
return !!param.call(element, element);
|
|
318
|
+
}
|
|
319
|
+
if (typeof param === 'string') {
|
|
320
|
+
try {
|
|
321
|
+
return !!element.form?.querySelector(param);
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
// not a selector
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
218
330
|
|
|
219
331
|
class ObjectStore {
|
|
220
332
|
constructor(data) {
|
|
221
333
|
this._store = {};
|
|
222
334
|
this._store = data;
|
|
335
|
+
this._defaults = { ...data };
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Whether the entry is still the built-in one (not changed with
|
|
339
|
+
* `set`, `replace` or `dv.localize`).
|
|
340
|
+
*/
|
|
341
|
+
isDefault(key) {
|
|
342
|
+
return key in this._defaults && this._store[key] === this._defaults[key];
|
|
223
343
|
}
|
|
224
344
|
keys() {
|
|
225
345
|
return Object.keys(this._store);
|
|
@@ -259,13 +379,17 @@ const store$1 = new ObjectStore({
|
|
|
259
379
|
regex,
|
|
260
380
|
nonalphamin: nonAlphaMin,
|
|
261
381
|
creditcard: creditCard,
|
|
382
|
+
extension,
|
|
383
|
+
remote,
|
|
262
384
|
});
|
|
263
385
|
/**
|
|
264
|
-
* Validates that the field has a value (is not blank).
|
|
265
|
-
*
|
|
386
|
+
* Validates that the field has a value (is not blank). The parameter can
|
|
387
|
+
* make it conditional: a boolean, a selector that must match in the form
|
|
388
|
+
* (e.g. '#other:checked'), or a function of the element.
|
|
389
|
+
* @return true if field is not blank, or the field isn't required now
|
|
266
390
|
*/
|
|
267
|
-
function required({ blank }) {
|
|
268
|
-
return !blank;
|
|
391
|
+
function required({ blank, param, element }) {
|
|
392
|
+
return !blank || !depend(param, element);
|
|
269
393
|
}
|
|
270
394
|
/**
|
|
271
395
|
* Validates that the field value has at least the minimum length.
|
|
@@ -381,8 +505,8 @@ function digits({ blank, value }) {
|
|
|
381
505
|
* @return true if values match
|
|
382
506
|
*/
|
|
383
507
|
function equalTo({ value, param }) {
|
|
384
|
-
const target = document.querySelector(param);
|
|
385
|
-
return value === target.value;
|
|
508
|
+
const target = param instanceof Element ? param : document.querySelector(param);
|
|
509
|
+
return !!target && value === target.value;
|
|
386
510
|
}
|
|
387
511
|
/**
|
|
388
512
|
* Validates that the field value matches a regular expression.
|
|
@@ -416,6 +540,10 @@ function creditCard({ blank, value }) {
|
|
|
416
540
|
if (blank) {
|
|
417
541
|
return true;
|
|
418
542
|
}
|
|
543
|
+
// only digits, spaces and dashes are allowed
|
|
544
|
+
if (/[^0-9 \-]+/.test(value)) {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
419
547
|
const strippedValue = value.replace(/\D/g, '');
|
|
420
548
|
let nCheck = 0;
|
|
421
549
|
let bEven = false;
|
|
@@ -437,6 +565,77 @@ function creditCard({ blank, value }) {
|
|
|
437
565
|
}
|
|
438
566
|
return nCheck % 10 === 0;
|
|
439
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* Validates that the field value (a file name) ends with one of the allowed extensions.
|
|
570
|
+
* Extensions are separated by commas or pipes, with or without a leading dot
|
|
571
|
+
* (e.g. "png,jpg", ".png,.jpg" or "png|jpe?g"). Defaults to common image types.
|
|
572
|
+
* For file inputs, every selected file is checked.
|
|
573
|
+
* @return true if blank or every file name has an allowed extension
|
|
574
|
+
*/
|
|
575
|
+
function extension({ blank, value, element, param, }) {
|
|
576
|
+
if (blank) {
|
|
577
|
+
return true;
|
|
578
|
+
}
|
|
579
|
+
const allowed = typeof param === 'string' && param.trim() !== ''
|
|
580
|
+
? param
|
|
581
|
+
.split(/[,|]/)
|
|
582
|
+
.map((ext) => ext.trim().replace(/^\./, ''))
|
|
583
|
+
.filter(Boolean)
|
|
584
|
+
.join('|')
|
|
585
|
+
: 'png|jpe?g|gif';
|
|
586
|
+
const re = new RegExp(`\\.(?:${allowed})$`, 'i');
|
|
587
|
+
const files = element.files;
|
|
588
|
+
if (files && files.length) {
|
|
589
|
+
return Array.from(files).every((file) => re.test(file.name));
|
|
590
|
+
}
|
|
591
|
+
return re.test(value);
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Asks the server whether the value is valid (e.g. "is this username free?").
|
|
595
|
+
* Sends the field as `name=value` (plus any `data`), GET by default, and
|
|
596
|
+
* expects JSON: true when valid; false, or an error message, when invalid.
|
|
597
|
+
* Results are cached by the validator until the value or data changes.
|
|
598
|
+
* @return true if blank, otherwise a promise of the server's answer
|
|
599
|
+
*/
|
|
600
|
+
function remote({ blank, value, element, param, signal, }) {
|
|
601
|
+
if (blank) {
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
604
|
+
const options = (typeof param === 'string' ? { url: param } : param);
|
|
605
|
+
const method = (options.type || options.method || 'GET').toUpperCase();
|
|
606
|
+
const fields = new URLSearchParams();
|
|
607
|
+
fields.append(element.name, value);
|
|
608
|
+
Object.entries(options.data ?? {}).forEach(([key, item]) => {
|
|
609
|
+
const fieldValue = typeof item === 'function' ? item() : item;
|
|
610
|
+
fields.set(key, fieldValue == null ? '' : String(fieldValue));
|
|
611
|
+
});
|
|
612
|
+
const url = new URL(options.url, document.baseURI);
|
|
613
|
+
const headers = { Accept: 'application/json' };
|
|
614
|
+
// like jQuery: marks the request as AJAX, but only on the same origin
|
|
615
|
+
// (on another origin it would need a CORS preflight)
|
|
616
|
+
if (url.origin === location.origin) {
|
|
617
|
+
headers['X-Requested-With'] = 'XMLHttpRequest';
|
|
618
|
+
}
|
|
619
|
+
const init = {
|
|
620
|
+
method,
|
|
621
|
+
headers: { ...headers, ...options.headers },
|
|
622
|
+
};
|
|
623
|
+
if (signal) {
|
|
624
|
+
init.signal = signal;
|
|
625
|
+
}
|
|
626
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
627
|
+
fields.forEach((fieldValue, key) => url.searchParams.set(key, fieldValue));
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
init.body = fields;
|
|
631
|
+
}
|
|
632
|
+
return fetch(url, init).then((response) => {
|
|
633
|
+
if (!response.ok) {
|
|
634
|
+
throw new Error(`remote: ${response.status} ${response.statusText}`);
|
|
635
|
+
}
|
|
636
|
+
return response.json();
|
|
637
|
+
});
|
|
638
|
+
}
|
|
440
639
|
|
|
441
640
|
/**
|
|
442
641
|
* Gets all validation rules for an element from multiple sources.
|
|
@@ -450,12 +649,23 @@ function getRules(element, settings) {
|
|
|
450
649
|
if (element == null || element.form == null) {
|
|
451
650
|
return {};
|
|
452
651
|
}
|
|
453
|
-
let data = normalizeRules(
|
|
652
|
+
let data = normalizeRules({
|
|
653
|
+
...classRules(element),
|
|
654
|
+
...attributeRules(element),
|
|
655
|
+
...dataRules(element),
|
|
656
|
+
...staticRules(element, settings),
|
|
657
|
+
}, element);
|
|
454
658
|
// Make sure required is at front
|
|
455
|
-
if (data
|
|
659
|
+
if ('required' in data) {
|
|
456
660
|
const param = data.required;
|
|
457
661
|
delete data.required;
|
|
458
|
-
data =
|
|
662
|
+
data = { required: param, ...data };
|
|
663
|
+
}
|
|
664
|
+
// and remote at the end: a pending request stops the other rules
|
|
665
|
+
if ('remote' in data) {
|
|
666
|
+
const param = data.remote;
|
|
667
|
+
delete data.remote;
|
|
668
|
+
data.remote = param;
|
|
459
669
|
}
|
|
460
670
|
return data;
|
|
461
671
|
}
|
|
@@ -481,19 +691,37 @@ function addClassRule(className) {
|
|
|
481
691
|
}
|
|
482
692
|
/**
|
|
483
693
|
* Normalizes and processes validation rules for an element.
|
|
484
|
-
* Removes false rules,
|
|
694
|
+
* Removes false rules, applies conditional rules (`{ param, depends }`),
|
|
695
|
+
* evaluates function parameters, and converts string/number values.
|
|
485
696
|
* @param rules - raw validation rules object
|
|
697
|
+
* @param element - the element the rules are for
|
|
486
698
|
* @return normalized rules object
|
|
487
699
|
*/
|
|
488
|
-
function normalizeRules(rules) {
|
|
700
|
+
function normalizeRules(rules, element) {
|
|
489
701
|
Object.entries(rules).forEach(([key, value]) => {
|
|
490
|
-
// Ignore rule when param is explicitly false, eg. required:false
|
|
491
|
-
|
|
702
|
+
// Ignore rule when param is explicitly false, eg. required:false,
|
|
703
|
+
// or missing, eg. an equalto whose other field isn't in the form
|
|
704
|
+
if (value === false || value === null || value === undefined) {
|
|
492
705
|
delete rules[key];
|
|
493
706
|
return;
|
|
494
707
|
}
|
|
495
|
-
|
|
496
|
-
|
|
708
|
+
// { param, depends }: the rule applies only when depends holds
|
|
709
|
+
if (typeof value === 'object' &&
|
|
710
|
+
!(value instanceof Element) &&
|
|
711
|
+
('param' in value || 'depends' in value)) {
|
|
712
|
+
const keep = !('depends' in value) || depend(value.depends, element);
|
|
713
|
+
if (keep) {
|
|
714
|
+
rules[key] = value.param !== undefined ? value.param : true;
|
|
715
|
+
}
|
|
716
|
+
else {
|
|
717
|
+
delete rules[key];
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
});
|
|
721
|
+
// a function parameter is called with the element, eg. required: () => …
|
|
722
|
+
Object.keys(rules).forEach((key) => {
|
|
723
|
+
if (typeof rules[key] === 'function') {
|
|
724
|
+
rules[key] = rules[key].call(element, element);
|
|
497
725
|
}
|
|
498
726
|
});
|
|
499
727
|
// Clean number parameters
|
|
@@ -533,7 +761,7 @@ function classRules(element) {
|
|
|
533
761
|
.map((className) => className.toLowerCase())
|
|
534
762
|
.forEach((className) => {
|
|
535
763
|
if (className in classRuleSettings) {
|
|
536
|
-
rules =
|
|
764
|
+
rules = { ...rules, ...classRuleSettings[className] };
|
|
537
765
|
}
|
|
538
766
|
});
|
|
539
767
|
}
|
|
@@ -574,14 +802,13 @@ function attributeRules(element) {
|
|
|
574
802
|
* @return validation rules derived from data attributes
|
|
575
803
|
*/
|
|
576
804
|
function dataRules(element) {
|
|
577
|
-
var _a;
|
|
578
805
|
const rules = {};
|
|
579
806
|
const type = element.type;
|
|
580
807
|
for (const method of store$1.keys()) {
|
|
581
808
|
const datasetKey = 'rule' +
|
|
582
809
|
method.charAt(0).toUpperCase() +
|
|
583
810
|
method.substring(1).toLowerCase();
|
|
584
|
-
let value =
|
|
811
|
+
let value = element.dataset[datasetKey] ?? null;
|
|
585
812
|
// Cast empty attributes like `data-rule-required` to `true`
|
|
586
813
|
if (value === '' || value === 'true') {
|
|
587
814
|
value = true;
|
|
@@ -671,6 +898,8 @@ const store = new ObjectStore({
|
|
|
671
898
|
regex: 'Please enter a value that matches the pattern {0}.',
|
|
672
899
|
nonalphamin: 'Please enter at least {0} non-alphabetic characters.',
|
|
673
900
|
creditcard: 'Please enter a valid credit card number.',
|
|
901
|
+
extension: 'Please enter a value with a valid extension.',
|
|
902
|
+
remote: 'Please fix this field.',
|
|
674
903
|
});
|
|
675
904
|
/**
|
|
676
905
|
* Gets the appropriate error message for a validation rule failure.
|
|
@@ -682,13 +911,17 @@ const store = new ObjectStore({
|
|
|
682
911
|
* @return formatted error message
|
|
683
912
|
*/
|
|
684
913
|
function getMessage(element, rule, ignoreTitle, customMessage) {
|
|
685
|
-
|
|
914
|
+
const message = [
|
|
686
915
|
customMessage,
|
|
687
916
|
customDataMessage(element, rule.method),
|
|
688
917
|
(!ignoreTitle && element.title) || undefined,
|
|
689
918
|
store.get(rule.method),
|
|
690
919
|
`<strong>Warning: No message defined for ${element.name}</strong>`,
|
|
691
920
|
].find((x) => x !== undefined);
|
|
921
|
+
// a function message is called with the parameters and the element
|
|
922
|
+
if (typeof message === 'function') {
|
|
923
|
+
return String(message.call(this, rule.parameters, element));
|
|
924
|
+
}
|
|
692
925
|
const regex = /\$?\{(\d+)}/g;
|
|
693
926
|
if (/\$?\{(\d+)}/g.test(message)) {
|
|
694
927
|
return format(message.replace(regex, '{$1}'), rule.parameters);
|
|
@@ -706,7 +939,7 @@ function getMessage(element, rule, ignoreTitle, customMessage) {
|
|
|
706
939
|
*/
|
|
707
940
|
function customDataMessage(element, method) {
|
|
708
941
|
const dataSetKey = 'msg' + method.charAt(0).toUpperCase() + method.substring(1).toLowerCase();
|
|
709
|
-
if (!
|
|
942
|
+
if (!element?.dataset) {
|
|
710
943
|
return;
|
|
711
944
|
}
|
|
712
945
|
return element.dataset[dataSetKey] || element.dataset['msg'];
|
|
@@ -720,6 +953,8 @@ const validatorStore = new WeakMap();
|
|
|
720
953
|
const EVENT_TARGETS = {
|
|
721
954
|
FOCUS_TARGETS: [
|
|
722
955
|
"[type='text']",
|
|
956
|
+
// an <input> without a type is a text input
|
|
957
|
+
'input:not([type])',
|
|
723
958
|
"[type='password']",
|
|
724
959
|
"[type='file']",
|
|
725
960
|
'select',
|
|
@@ -749,19 +984,22 @@ const EVENT_TARGETS = {
|
|
|
749
984
|
* Handles attaching and removing event listeners with proper delegation logic.
|
|
750
985
|
*/
|
|
751
986
|
class FormEventManager {
|
|
752
|
-
constructor(form, settings, validateCallback, focusInvalidCallback, shouldIgnoreElement
|
|
987
|
+
constructor(form, settings, validateCallback, focusInvalidCallback, shouldIgnoreElement,
|
|
988
|
+
/** holds the submit while async validations run; true if held */
|
|
989
|
+
holdSubmitCallback = () => false) {
|
|
753
990
|
this.form = form;
|
|
754
991
|
this.settings = settings;
|
|
755
992
|
this.validateCallback = validateCallback;
|
|
756
993
|
this.focusInvalidCallback = focusInvalidCallback;
|
|
757
994
|
this.shouldIgnoreElement = shouldIgnoreElement;
|
|
995
|
+
this.holdSubmitCallback = holdSubmitCallback;
|
|
758
996
|
this.boundEventHandlers = {
|
|
759
997
|
onFocusIn: null,
|
|
760
998
|
onFocusOut: null,
|
|
761
999
|
onKeyUp: null,
|
|
762
1000
|
onClick: null,
|
|
763
|
-
onClickSubmit: null,
|
|
764
1001
|
onSubmit: null,
|
|
1002
|
+
onInvalidForm: null,
|
|
765
1003
|
};
|
|
766
1004
|
this.submitButton = null;
|
|
767
1005
|
}
|
|
@@ -778,19 +1016,18 @@ class FormEventManager {
|
|
|
778
1016
|
this.form.addEventListener('keyup', this.boundEventHandlers.onKeyUp);
|
|
779
1017
|
this.form.addEventListener('click', this.boundEventHandlers.onClick);
|
|
780
1018
|
if (this.settings.onsubmit) {
|
|
781
|
-
this.boundEventHandlers.onClickSubmit = this.handleSubmitClick;
|
|
782
1019
|
this.boundEventHandlers.onSubmit = (e) => this.handleSubmitForm(e);
|
|
783
|
-
this.form.addEventListener('click', this.boundEventHandlers.onClick);
|
|
784
1020
|
this.form.addEventListener('submit', this.boundEventHandlers.onSubmit);
|
|
785
1021
|
}
|
|
1022
|
+
// the validator wraps it to pass itself as the second argument
|
|
786
1023
|
const invalidHandler = this.settings.invalidHandler;
|
|
787
1024
|
if (invalidHandler) {
|
|
788
|
-
this.
|
|
789
|
-
if (
|
|
790
|
-
|
|
1025
|
+
this.boundEventHandlers.onInvalidForm = (e) => {
|
|
1026
|
+
if (this.isCustomEvent(e)) {
|
|
1027
|
+
invalidHandler(e);
|
|
791
1028
|
}
|
|
792
|
-
|
|
793
|
-
|
|
1029
|
+
};
|
|
1030
|
+
this.form.addEventListener('invalid-form', this.boundEventHandlers.onInvalidForm);
|
|
794
1031
|
}
|
|
795
1032
|
}
|
|
796
1033
|
triggerInvalidForm(errorList) {
|
|
@@ -803,29 +1040,42 @@ class FormEventManager {
|
|
|
803
1040
|
isCustomEvent(event) {
|
|
804
1041
|
return 'detail' in event;
|
|
805
1042
|
}
|
|
806
|
-
handleSubmitClick(e) {
|
|
807
|
-
const target = e.target;
|
|
808
|
-
// Track the used submit button to properly handle scripted
|
|
809
|
-
// submits later.
|
|
810
|
-
if (target.matches('button[type="submit"], input[type="submit"]')) {
|
|
811
|
-
this.submitButton = e.target;
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
1043
|
handleSubmitForm(e) {
|
|
815
1044
|
if (this.settings.debug) {
|
|
816
1045
|
e.preventDefault();
|
|
817
1046
|
}
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
1047
|
+
// the button used to submit, so its name/value can be sent along
|
|
1048
|
+
const submitter = e.submitter;
|
|
1049
|
+
this.submitButton =
|
|
1050
|
+
submitter instanceof HTMLButtonElement ||
|
|
1051
|
+
submitter instanceof HTMLInputElement
|
|
1052
|
+
? submitter
|
|
1053
|
+
: null;
|
|
1054
|
+
// a button with formnovalidate or class="cancel" (Cancel, Back, Save
|
|
1055
|
+
// draft) submits without validation, but still runs the submitHandler
|
|
1056
|
+
const skipValidation = !!this.submitButton &&
|
|
1057
|
+
(this.submitButton.hasAttribute('formnovalidate') ||
|
|
1058
|
+
this.submitButton.classList.contains('cancel'));
|
|
1059
|
+
if (!skipValidation) {
|
|
1060
|
+
const isValid = this.validateCallback();
|
|
1061
|
+
if (!isValid) {
|
|
1062
|
+
this.focusInvalidCallback();
|
|
1063
|
+
e.preventDefault();
|
|
1064
|
+
e.stopPropagation();
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
// async validations (e.g. remote) are running: wait for them, then
|
|
1068
|
+
// the validator submits again if everything is valid
|
|
1069
|
+
if (this.holdSubmitCallback(submitter ?? null)) {
|
|
1070
|
+
e.preventDefault();
|
|
1071
|
+
e.stopPropagation();
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
824
1074
|
}
|
|
825
1075
|
let hidden = null;
|
|
826
1076
|
// Insert a hidden input as a replacement for the missing submit button
|
|
827
1077
|
// if a submitHandler is passed as option
|
|
828
|
-
if (this.submitButton && this.settings.submitHandler) {
|
|
1078
|
+
if (this.submitButton?.name && this.settings.submitHandler) {
|
|
829
1079
|
hidden = document.createElement('input');
|
|
830
1080
|
hidden.setAttribute('type', 'hidden');
|
|
831
1081
|
hidden.setAttribute('name', this.submitButton.name);
|
|
@@ -860,19 +1110,19 @@ class FormEventManager {
|
|
|
860
1110
|
if (this.boundEventHandlers.onClick) {
|
|
861
1111
|
this.form.removeEventListener('click', this.boundEventHandlers.onClick);
|
|
862
1112
|
}
|
|
863
|
-
if (this.boundEventHandlers.onClickSubmit) {
|
|
864
|
-
this.form.removeEventListener('click', this.boundEventHandlers.onClickSubmit);
|
|
865
|
-
}
|
|
866
1113
|
if (this.boundEventHandlers.onSubmit) {
|
|
867
1114
|
this.form.removeEventListener('submit', this.boundEventHandlers.onSubmit);
|
|
868
1115
|
}
|
|
1116
|
+
if (this.boundEventHandlers.onInvalidForm) {
|
|
1117
|
+
this.form.removeEventListener('invalid-form', this.boundEventHandlers.onInvalidForm);
|
|
1118
|
+
}
|
|
869
1119
|
// Clear references
|
|
870
1120
|
this.boundEventHandlers.onFocusIn = null;
|
|
871
1121
|
this.boundEventHandlers.onFocusOut = null;
|
|
872
1122
|
this.boundEventHandlers.onKeyUp = null;
|
|
873
1123
|
this.boundEventHandlers.onClick = null;
|
|
874
|
-
this.boundEventHandlers.onClickSubmit = null;
|
|
875
1124
|
this.boundEventHandlers.onSubmit = null;
|
|
1125
|
+
this.boundEventHandlers.onInvalidForm = null;
|
|
876
1126
|
}
|
|
877
1127
|
/**
|
|
878
1128
|
* Creates a delegated event handler for the specified targets and handler
|
|
@@ -920,6 +1170,7 @@ const ValidatorDefaults = {
|
|
|
920
1170
|
ignore: ':hidden',
|
|
921
1171
|
errorClass: 'error',
|
|
922
1172
|
validClass: 'valid',
|
|
1173
|
+
pendingClass: 'pending',
|
|
923
1174
|
errorElement: 'label',
|
|
924
1175
|
wrapper: null,
|
|
925
1176
|
errorLabelContainer: null,
|
|
@@ -1025,6 +1276,40 @@ function unhighlight(element, errorClasses, validClasses) {
|
|
|
1025
1276
|
});
|
|
1026
1277
|
}
|
|
1027
1278
|
|
|
1279
|
+
function isPromiseLike(value) {
|
|
1280
|
+
return !!value && typeof value.then === 'function';
|
|
1281
|
+
}
|
|
1282
|
+
/** Identifies the input of an async method: its values and parameters. */
|
|
1283
|
+
function asyncKey(values, param) {
|
|
1284
|
+
try {
|
|
1285
|
+
return JSON.stringify([values, param], (_key, value) => typeof value === 'function' ? value() : value);
|
|
1286
|
+
}
|
|
1287
|
+
catch {
|
|
1288
|
+
return JSON.stringify(values);
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
/**
|
|
1292
|
+
* Methods that, like jQuery's, pass a blank field as "optional" (jQuery's
|
|
1293
|
+
* "dependency-mismatch"). When one is a blank field's only rule, the field is
|
|
1294
|
+
* neutral: neither an error nor a success.
|
|
1295
|
+
*/
|
|
1296
|
+
const OPTIONAL_METHODS = new Set([
|
|
1297
|
+
'minlength',
|
|
1298
|
+
'maxlength',
|
|
1299
|
+
'rangelength',
|
|
1300
|
+
'min',
|
|
1301
|
+
'max',
|
|
1302
|
+
'range',
|
|
1303
|
+
'email',
|
|
1304
|
+
'url',
|
|
1305
|
+
'date',
|
|
1306
|
+
'unobtrusivedate',
|
|
1307
|
+
'number',
|
|
1308
|
+
'digits',
|
|
1309
|
+
'creditcard',
|
|
1310
|
+
'extension',
|
|
1311
|
+
'remote',
|
|
1312
|
+
]);
|
|
1028
1313
|
class Validator {
|
|
1029
1314
|
/**
|
|
1030
1315
|
* Initializes a new validator instance for the given form with optional configuration settings.
|
|
@@ -1033,6 +1318,7 @@ class Validator {
|
|
|
1033
1318
|
*/
|
|
1034
1319
|
constructor(form, options) {
|
|
1035
1320
|
this.submitted = {};
|
|
1321
|
+
/** fields validated so far: their message, or false when valid */
|
|
1036
1322
|
this.invalid = {};
|
|
1037
1323
|
this.successList = [];
|
|
1038
1324
|
this.errorList = [];
|
|
@@ -1045,9 +1331,27 @@ class Validator {
|
|
|
1045
1331
|
this.lastActive = null;
|
|
1046
1332
|
/** normalized rules */
|
|
1047
1333
|
this.rules = {};
|
|
1334
|
+
/** whether init() added the novalidate attribute (so destroy() removes it) */
|
|
1335
|
+
this.addedNoValidate = false;
|
|
1336
|
+
/** number of async validations (e.g. remote) still running */
|
|
1337
|
+
this.pendingRequest = 0;
|
|
1338
|
+
/** running async validations by field name */
|
|
1339
|
+
this.pending = {};
|
|
1340
|
+
/** cached async results by field name, then method */
|
|
1341
|
+
this.asyncResults = {};
|
|
1342
|
+
/** a submit is waiting for the running async validations */
|
|
1343
|
+
this.formSubmitted = false;
|
|
1344
|
+
/** the button that started the held submit */
|
|
1345
|
+
this.heldSubmitter = null;
|
|
1346
|
+
/** while > 0, cached failed requests are reused instead of retried */
|
|
1347
|
+
this.reuseFailed = 0;
|
|
1348
|
+
/** fields whose equalto rule has run: rechecked when the other field blurs */
|
|
1349
|
+
this.equalToChecked = new Set();
|
|
1350
|
+
/** callbacks waiting for pendingRequest to reach 0 */
|
|
1351
|
+
this.settleWaiters = [];
|
|
1048
1352
|
this.settings = ValidatorDefaults;
|
|
1049
1353
|
this.currentForm = form;
|
|
1050
|
-
this.settings =
|
|
1354
|
+
this.settings = { ...this.settings, ...options };
|
|
1051
1355
|
this.normalizeSettings();
|
|
1052
1356
|
this.init();
|
|
1053
1357
|
}
|
|
@@ -1065,7 +1369,6 @@ class Validator {
|
|
|
1065
1369
|
'highlight',
|
|
1066
1370
|
'unhighlight',
|
|
1067
1371
|
'errorPlacement',
|
|
1068
|
-
'invalidHandler',
|
|
1069
1372
|
'submitHandler',
|
|
1070
1373
|
].forEach((key) => {
|
|
1071
1374
|
const setting = this.settings[key];
|
|
@@ -1073,6 +1376,14 @@ class Validator {
|
|
|
1073
1376
|
this.settings[key] = setting.bind(this);
|
|
1074
1377
|
}
|
|
1075
1378
|
});
|
|
1379
|
+
// like jQuery: invalidHandler(event, validator)
|
|
1380
|
+
const invalidHandler = this.settings.invalidHandler;
|
|
1381
|
+
if (invalidHandler) {
|
|
1382
|
+
this.settings.invalidHandler = (event) => invalidHandler.call(this, event, this);
|
|
1383
|
+
}
|
|
1384
|
+
// give each validator its own messages map, so setRules() never writes
|
|
1385
|
+
// into the shared defaults or the caller's options object
|
|
1386
|
+
this.settings.messages = { ...this.settings.messages };
|
|
1076
1387
|
// transform method names to lowercase in messages
|
|
1077
1388
|
if (this.settings.messages) {
|
|
1078
1389
|
Object.keys(this.settings.messages).forEach((elementName) => {
|
|
@@ -1093,22 +1404,68 @@ class Validator {
|
|
|
1093
1404
|
* Sets up DOM references and attaches event handlers to the form.
|
|
1094
1405
|
*/
|
|
1095
1406
|
init() {
|
|
1407
|
+
// turn off the browser's own validation (popups, blocked submits),
|
|
1408
|
+
// which would otherwise run before ours for required, type="email", etc.
|
|
1409
|
+
if (!this.currentForm.hasAttribute('novalidate')) {
|
|
1410
|
+
this.currentForm.setAttribute('novalidate', 'novalidate');
|
|
1411
|
+
this.addedNoValidate = true;
|
|
1412
|
+
}
|
|
1096
1413
|
if (this.settings.errorLabelContainer) {
|
|
1097
1414
|
this.labelContainer = document.querySelector(this.settings.errorLabelContainer);
|
|
1098
1415
|
}
|
|
1099
1416
|
this.errorContext = this.labelContainer || this.currentForm;
|
|
1417
|
+
// every element errorContainer matches is shown and hidden; error labels
|
|
1418
|
+
// only go in the first element errorLabelContainer matches
|
|
1100
1419
|
const containers = [this.labelContainer];
|
|
1101
1420
|
if (this.settings.errorContainer) {
|
|
1102
|
-
containers.push(document.
|
|
1421
|
+
containers.push(...document.querySelectorAll(this.settings.errorContainer));
|
|
1103
1422
|
}
|
|
1104
1423
|
this.containers = containers.filter(Boolean);
|
|
1105
1424
|
this.rules = {};
|
|
1106
1425
|
Object.entries(this.settings.rules).forEach(([key, value]) => {
|
|
1107
1426
|
this.rules[key] = normalizeRule(value);
|
|
1108
1427
|
});
|
|
1109
|
-
|
|
1428
|
+
// like jQuery's equalTo: leaving the other field rechecks this one
|
|
1429
|
+
const onfocusout = this.settings.onfocusout;
|
|
1430
|
+
if (onfocusout) {
|
|
1431
|
+
this.settings.onfocusout = (element, event) => {
|
|
1432
|
+
onfocusout(element, event);
|
|
1433
|
+
this.recheckEqualTo(element);
|
|
1434
|
+
};
|
|
1435
|
+
}
|
|
1436
|
+
this.eventManager = new FormEventManager(this.currentForm, this.settings, () => this.form(), () => this.focusInvalid(), (element) => this.shouldIgnore(element), (submitter) => this.holdSubmit(submitter));
|
|
1110
1437
|
this.eventManager.attachEventHandlers();
|
|
1111
1438
|
}
|
|
1439
|
+
/**
|
|
1440
|
+
* Sets the rules (and optionally the messages) for a field by name after
|
|
1441
|
+
* the validator has been created, replacing any rules set for it before.
|
|
1442
|
+
* Useful when fields are added to the form dynamically.
|
|
1443
|
+
* Rules from HTML attributes, data attributes and classes still apply.
|
|
1444
|
+
* @param name - name attribute of the field
|
|
1445
|
+
* @param rules - rules for the field, or null to remove them
|
|
1446
|
+
* @param messages - custom messages for the field, keyed by method name
|
|
1447
|
+
* @example
|
|
1448
|
+
* validator.setRules('email', { required: true, email: true }, {
|
|
1449
|
+
* required: 'We need your email.',
|
|
1450
|
+
* });
|
|
1451
|
+
*/
|
|
1452
|
+
setRules(name, rules, messages) {
|
|
1453
|
+
if (rules === null) {
|
|
1454
|
+
delete this.rules[name];
|
|
1455
|
+
delete this.settings.messages[name];
|
|
1456
|
+
return;
|
|
1457
|
+
}
|
|
1458
|
+
this.rules[name] = normalizeRule(rules);
|
|
1459
|
+
if (typeof messages === 'string') {
|
|
1460
|
+
this.settings.messages[name] = messages;
|
|
1461
|
+
}
|
|
1462
|
+
else if (messages) {
|
|
1463
|
+
this.settings.messages[name] = Object.fromEntries(Object.entries(messages).map(([method, msg]) => [
|
|
1464
|
+
method.toLowerCase(),
|
|
1465
|
+
msg,
|
|
1466
|
+
]));
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1112
1469
|
/**
|
|
1113
1470
|
* Resets the validator state.
|
|
1114
1471
|
*/
|
|
@@ -1125,17 +1482,19 @@ class Validator {
|
|
|
1125
1482
|
* @return true if the form is valid, false otherwise
|
|
1126
1483
|
*/
|
|
1127
1484
|
form() {
|
|
1485
|
+
this.retryFailedRequests();
|
|
1128
1486
|
this.prepareForm();
|
|
1129
1487
|
this.currentElements = this.elements();
|
|
1130
1488
|
for (const element of this.currentElements) {
|
|
1131
1489
|
this.check(element);
|
|
1132
1490
|
}
|
|
1133
|
-
this.submitted =
|
|
1134
|
-
this.invalid =
|
|
1491
|
+
this.submitted = { ...this.submitted, ...this.errorMap };
|
|
1492
|
+
this.invalid = { ...this.errorMap };
|
|
1135
1493
|
if (!this.valid()) {
|
|
1136
1494
|
this.eventManager.triggerInvalidForm(this.errorList);
|
|
1137
1495
|
}
|
|
1138
1496
|
this.showErrors();
|
|
1497
|
+
this.notifySettled();
|
|
1139
1498
|
return this.valid();
|
|
1140
1499
|
}
|
|
1141
1500
|
/**
|
|
@@ -1144,6 +1503,7 @@ class Validator {
|
|
|
1144
1503
|
* @return true if the element is valid, false otherwise
|
|
1145
1504
|
*/
|
|
1146
1505
|
element(element) {
|
|
1506
|
+
this.retryFailedRequests();
|
|
1147
1507
|
const target = this.validationTargetFor(element);
|
|
1148
1508
|
if (target === undefined) {
|
|
1149
1509
|
delete this.invalid[element.name];
|
|
@@ -1152,18 +1512,16 @@ class Validator {
|
|
|
1152
1512
|
this.prepareElement(target);
|
|
1153
1513
|
this.currentElements = [target];
|
|
1154
1514
|
const result = this.check(target);
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
else {
|
|
1159
|
-
this.invalid[target.name] = 'Invalid';
|
|
1160
|
-
}
|
|
1515
|
+
// keep valid fields as false (not deleted): onkeyup revalidates every
|
|
1516
|
+
// field that has been validated once, as jQuery does
|
|
1517
|
+
this.invalid[target.name] = result ? false : 'Invalid';
|
|
1161
1518
|
if (!this.numberOfInvalids()) {
|
|
1162
1519
|
this.toHide.push(...this.containers);
|
|
1163
1520
|
}
|
|
1164
1521
|
this.showErrors();
|
|
1165
1522
|
// add aria-invalid status for screen readers
|
|
1166
1523
|
element.setAttribute('aria-invalid', (!result).toString());
|
|
1524
|
+
this.notifySettled();
|
|
1167
1525
|
return result;
|
|
1168
1526
|
}
|
|
1169
1527
|
prepareElement(element) {
|
|
@@ -1191,9 +1549,11 @@ class Validator {
|
|
|
1191
1549
|
elements() {
|
|
1192
1550
|
const rulesCache = {};
|
|
1193
1551
|
const selector = 'input, select, textarea';
|
|
1194
|
-
|
|
1552
|
+
// :disabled also matches fields inside <fieldset disabled>
|
|
1553
|
+
const notSelector = 'input[type="submit"], [type="reset"], [type="image"], :disabled';
|
|
1195
1554
|
const elements = Array.from(this.currentForm.querySelectorAll(selector)).filter((el) => !el.matches(notSelector) && !this.shouldIgnore(el));
|
|
1196
|
-
|
|
1555
|
+
// getAttribute: a field named "id" shadows form.id
|
|
1556
|
+
const formId = this.currentForm.getAttribute('id');
|
|
1197
1557
|
if (formId) {
|
|
1198
1558
|
const escapedId = escapeCssMeta(formId);
|
|
1199
1559
|
const outsideElements = Array.from(document.querySelectorAll(selector)).filter((el) => el.matches(`[form="${escapedId}"]`) &&
|
|
@@ -1222,7 +1582,8 @@ class Validator {
|
|
|
1222
1582
|
*/
|
|
1223
1583
|
validElements() {
|
|
1224
1584
|
const invalid = this.invalidElements();
|
|
1225
|
-
|
|
1585
|
+
// fields with a running async validation are not known to be valid yet
|
|
1586
|
+
return this.currentElements.filter((el) => !invalid.includes(el) && !(el.name in this.pending));
|
|
1226
1587
|
}
|
|
1227
1588
|
/**
|
|
1228
1589
|
* Returns an array of currently invalid elements from the last validation.
|
|
@@ -1237,14 +1598,24 @@ class Validator {
|
|
|
1237
1598
|
* @return true if the element is valid, false otherwise
|
|
1238
1599
|
*/
|
|
1239
1600
|
check(element) {
|
|
1240
|
-
var _a;
|
|
1241
1601
|
element = this.validationTargetFor(element);
|
|
1242
1602
|
const rules = getRules(element, this.rules);
|
|
1243
1603
|
const elValue = elementValue(element, this.currentForm);
|
|
1244
|
-
const value = (
|
|
1604
|
+
const value = (Array.isArray(elValue) ? elValue[0] : elValue) ?? '';
|
|
1245
1605
|
const values = Array.isArray(elValue) ? elValue : [elValue];
|
|
1246
1606
|
const length = getValueLength(element, this.currentForm);
|
|
1247
1607
|
const blank = isBlankElement(element, this.currentForm);
|
|
1608
|
+
// a running async validation for a previous value is out of date
|
|
1609
|
+
const running = this.pending[element.name];
|
|
1610
|
+
if (running &&
|
|
1611
|
+
(!(running.method in rules) ||
|
|
1612
|
+
running.key !== asyncKey(values, rules[running.method]))) {
|
|
1613
|
+
this.abortRequest(element);
|
|
1614
|
+
}
|
|
1615
|
+
// a lone rule that doesn't apply (blank optional field, or required
|
|
1616
|
+
// whose condition is false) leaves the field neutral: no success
|
|
1617
|
+
const ruleCount = Object.keys(rules).length;
|
|
1618
|
+
let neutral = false;
|
|
1248
1619
|
for (const method in rules) {
|
|
1249
1620
|
const rule = { method, parameters: rules[method] };
|
|
1250
1621
|
const methodFunc = store$1.get(method);
|
|
@@ -1252,7 +1623,25 @@ class Validator {
|
|
|
1252
1623
|
console.warn(`Validation method "${method}" not found. Skipping.`);
|
|
1253
1624
|
continue;
|
|
1254
1625
|
}
|
|
1255
|
-
|
|
1626
|
+
// async methods: reuse the result while the value is unchanged
|
|
1627
|
+
const cached = this.asyncResults[element.name]?.[method];
|
|
1628
|
+
if (cached && cached.key === asyncKey(values, rule.parameters)) {
|
|
1629
|
+
if (cached.result === true) {
|
|
1630
|
+
continue;
|
|
1631
|
+
}
|
|
1632
|
+
// like jQuery, remote's own message is formatted with the value
|
|
1633
|
+
// ("'{0}' is already taken"), not with the request options
|
|
1634
|
+
this.formatAndAdd(element, rule, cached.result ??
|
|
1635
|
+
(method === 'remote'
|
|
1636
|
+
? this.getMessage(element, { method, parameters: value })
|
|
1637
|
+
: undefined));
|
|
1638
|
+
return false;
|
|
1639
|
+
}
|
|
1640
|
+
if (method === 'equalto') {
|
|
1641
|
+
this.equalToChecked.add(element.name);
|
|
1642
|
+
}
|
|
1643
|
+
if (this.pending[element.name]?.method !== method) {
|
|
1644
|
+
const controller = new AbortController();
|
|
1256
1645
|
const result = methodFunc({
|
|
1257
1646
|
blank,
|
|
1258
1647
|
value: value,
|
|
@@ -1260,21 +1649,189 @@ class Validator {
|
|
|
1260
1649
|
length,
|
|
1261
1650
|
element,
|
|
1262
1651
|
param: rule.parameters,
|
|
1652
|
+
signal: controller.signal,
|
|
1263
1653
|
});
|
|
1264
|
-
if (!result) {
|
|
1265
|
-
|
|
1266
|
-
|
|
1654
|
+
if (!isPromiseLike(result)) {
|
|
1655
|
+
if (!result) {
|
|
1656
|
+
this.formatAndAdd(element, rule);
|
|
1657
|
+
return false;
|
|
1658
|
+
}
|
|
1659
|
+
neutral =
|
|
1660
|
+
ruleCount === 1 &&
|
|
1661
|
+
(method === 'required'
|
|
1662
|
+
? !depend(rule.parameters, element)
|
|
1663
|
+
: blank && OPTIONAL_METHODS.has(method));
|
|
1664
|
+
continue;
|
|
1267
1665
|
}
|
|
1666
|
+
const key = asyncKey(values, rule.parameters);
|
|
1667
|
+
this.startRequest(element, { controller, method, key });
|
|
1668
|
+
result.then((response) => this.finishRequest(element, key, controller, response), () => this.finishRequest(element, key, controller, null));
|
|
1268
1669
|
}
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1670
|
+
// Pending: not an error (yet). Keep the current message visible and
|
|
1671
|
+
// stop here; the field is validated again when the answer arrives.
|
|
1672
|
+
const shown = this.errorsFor(element);
|
|
1673
|
+
this.toHide = this.toHide.filter((el) => !shown.includes(el));
|
|
1674
|
+
return true;
|
|
1272
1675
|
}
|
|
1273
|
-
if (objectLength(rules)) {
|
|
1676
|
+
if (!neutral && objectLength(rules)) {
|
|
1274
1677
|
this.successList.push(element);
|
|
1275
1678
|
}
|
|
1276
1679
|
return true;
|
|
1277
1680
|
}
|
|
1681
|
+
/**
|
|
1682
|
+
* Validates again the fields whose equalto rule points at the element
|
|
1683
|
+
* (and has run before), e.g. ConfirmPassword when Password changes.
|
|
1684
|
+
*/
|
|
1685
|
+
recheckEqualTo(element) {
|
|
1686
|
+
this.equalToChecked.forEach((name) => {
|
|
1687
|
+
const field = findByName(this.currentForm, name)[0];
|
|
1688
|
+
if (!field || field === element) {
|
|
1689
|
+
return;
|
|
1690
|
+
}
|
|
1691
|
+
const param = getRules(field, this.rules)['equalto'];
|
|
1692
|
+
let target = null;
|
|
1693
|
+
try {
|
|
1694
|
+
target =
|
|
1695
|
+
param instanceof Element ? param : document.querySelector(param);
|
|
1696
|
+
}
|
|
1697
|
+
catch {
|
|
1698
|
+
// not a selector: the equalto rule itself reports it
|
|
1699
|
+
}
|
|
1700
|
+
if (target === element) {
|
|
1701
|
+
this.element(field);
|
|
1702
|
+
}
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
/**
|
|
1706
|
+
* Returns a promise that resolves once no async validations are running.
|
|
1707
|
+
*/
|
|
1708
|
+
settled() {
|
|
1709
|
+
if (this.pendingRequest === 0) {
|
|
1710
|
+
return Promise.resolve();
|
|
1711
|
+
}
|
|
1712
|
+
return new Promise((resolve) => this.settleWaiters.push(resolve));
|
|
1713
|
+
}
|
|
1714
|
+
/**
|
|
1715
|
+
* Runs fn reusing the results of failed requests instead of retrying them.
|
|
1716
|
+
* @internal
|
|
1717
|
+
*/
|
|
1718
|
+
withCachedResults(fn) {
|
|
1719
|
+
this.reuseFailed++;
|
|
1720
|
+
try {
|
|
1721
|
+
return fn();
|
|
1722
|
+
}
|
|
1723
|
+
finally {
|
|
1724
|
+
this.reuseFailed--;
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
retryFailedRequests() {
|
|
1728
|
+
if (this.reuseFailed) {
|
|
1729
|
+
return;
|
|
1730
|
+
}
|
|
1731
|
+
Object.values(this.asyncResults).forEach((byMethod) => {
|
|
1732
|
+
Object.keys(byMethod).forEach((method) => {
|
|
1733
|
+
if (byMethod[method].failed) {
|
|
1734
|
+
delete byMethod[method];
|
|
1735
|
+
}
|
|
1736
|
+
});
|
|
1737
|
+
});
|
|
1738
|
+
}
|
|
1739
|
+
startRequest(element, request) {
|
|
1740
|
+
this.abortRequest(element);
|
|
1741
|
+
this.pending[element.name] = request;
|
|
1742
|
+
this.pendingRequest++;
|
|
1743
|
+
if (this.settings.pendingClass) {
|
|
1744
|
+
element.classList.add(this.settings.pendingClass);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
abortRequest(element) {
|
|
1748
|
+
const running = this.pending[element.name];
|
|
1749
|
+
if (!running) {
|
|
1750
|
+
return;
|
|
1751
|
+
}
|
|
1752
|
+
running.controller.abort();
|
|
1753
|
+
this.endRequest(element);
|
|
1754
|
+
// the user changed the field while a submit was waiting: drop the submit
|
|
1755
|
+
if (this.pendingRequest === 0) {
|
|
1756
|
+
this.formSubmitted = false;
|
|
1757
|
+
this.heldSubmitter = null;
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
endRequest(element) {
|
|
1761
|
+
delete this.pending[element.name];
|
|
1762
|
+
this.pendingRequest = Math.max(0, this.pendingRequest - 1);
|
|
1763
|
+
if (this.settings.pendingClass) {
|
|
1764
|
+
element.classList.remove(this.settings.pendingClass);
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
finishRequest(element, key, controller, response) {
|
|
1768
|
+
var _a, _b;
|
|
1769
|
+
// a newer validation, a reset or destroy() replaced this one
|
|
1770
|
+
const running = this.pending[element.name];
|
|
1771
|
+
if (controller.signal.aborted || running?.controller !== controller) {
|
|
1772
|
+
return;
|
|
1773
|
+
}
|
|
1774
|
+
this.endRequest(element);
|
|
1775
|
+
const valid = response === true || response === 'true';
|
|
1776
|
+
((_a = this.asyncResults)[_b = element.name] ?? (_a[_b] = {}))[running.method] = {
|
|
1777
|
+
key,
|
|
1778
|
+
result: valid
|
|
1779
|
+
? true
|
|
1780
|
+
: typeof response === 'string' && response !== ''
|
|
1781
|
+
? response
|
|
1782
|
+
: null,
|
|
1783
|
+
failed: response === null,
|
|
1784
|
+
};
|
|
1785
|
+
// validate the field again: the answer is cached now, and any rules
|
|
1786
|
+
// after the async one run too (unless the field or validator is gone)
|
|
1787
|
+
if (validatorStore.get(this.currentForm) === this && element.isConnected) {
|
|
1788
|
+
this.withCachedResults(() => this.element(element));
|
|
1789
|
+
}
|
|
1790
|
+
if (this.pendingRequest === 0) {
|
|
1791
|
+
this.resumeSubmit();
|
|
1792
|
+
this.notifySettled();
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
/** Called on submit while async validations are running. */
|
|
1796
|
+
holdSubmit(submitter) {
|
|
1797
|
+
if (this.pendingRequest === 0) {
|
|
1798
|
+
return false;
|
|
1799
|
+
}
|
|
1800
|
+
this.formSubmitted = true;
|
|
1801
|
+
this.heldSubmitter = submitter;
|
|
1802
|
+
return true;
|
|
1803
|
+
}
|
|
1804
|
+
/** Submits the held submit once everything is valid, as the user meant to. */
|
|
1805
|
+
resumeSubmit() {
|
|
1806
|
+
if (!this.formSubmitted) {
|
|
1807
|
+
return;
|
|
1808
|
+
}
|
|
1809
|
+
this.formSubmitted = false;
|
|
1810
|
+
const submitter = this.heldSubmitter;
|
|
1811
|
+
this.heldSubmitter = null;
|
|
1812
|
+
this.withCachedResults(() => {
|
|
1813
|
+
if (!this.form()) {
|
|
1814
|
+
this.focusInvalid();
|
|
1815
|
+
return;
|
|
1816
|
+
}
|
|
1817
|
+
if (this.holdSubmit(submitter)) {
|
|
1818
|
+
return;
|
|
1819
|
+
}
|
|
1820
|
+
const form = this.currentForm;
|
|
1821
|
+
const isSubmitter = submitter instanceof HTMLElement &&
|
|
1822
|
+
submitter.form === form &&
|
|
1823
|
+
submitter.matches('button[type="submit"], button:not([type]), input[type="submit"], input[type="image"]');
|
|
1824
|
+
form.requestSubmit(isSubmitter ? submitter : undefined);
|
|
1825
|
+
});
|
|
1826
|
+
}
|
|
1827
|
+
notifySettled() {
|
|
1828
|
+
if (this.pendingRequest !== 0) {
|
|
1829
|
+
return;
|
|
1830
|
+
}
|
|
1831
|
+
const waiters = this.settleWaiters;
|
|
1832
|
+
this.settleWaiters = [];
|
|
1833
|
+
waiters.forEach((resolve) => resolve());
|
|
1834
|
+
}
|
|
1278
1835
|
/**
|
|
1279
1836
|
* Returns all error label elements currently in the DOM.
|
|
1280
1837
|
* @return array of error label elements
|
|
@@ -1286,7 +1843,7 @@ class Validator {
|
|
|
1286
1843
|
showErrors(errors) {
|
|
1287
1844
|
if (errors) {
|
|
1288
1845
|
// Add items to error list and map
|
|
1289
|
-
this.errorMap =
|
|
1846
|
+
this.errorMap = { ...this.errorMap, ...errors };
|
|
1290
1847
|
this.errorList = Object.entries(this.errorMap).map(([name, message]) => ({
|
|
1291
1848
|
message,
|
|
1292
1849
|
method: message,
|
|
@@ -1324,8 +1881,7 @@ class Validator {
|
|
|
1324
1881
|
this.hideErrors();
|
|
1325
1882
|
this.addWrapper(...this.toShow).forEach(showElement);
|
|
1326
1883
|
}
|
|
1327
|
-
formatAndAdd(element, rule) {
|
|
1328
|
-
const message = this.getMessage(element, rule);
|
|
1884
|
+
formatAndAdd(element, rule, message = this.getMessage(element, rule)) {
|
|
1329
1885
|
this.errorList.push({ message, element, method: rule.method });
|
|
1330
1886
|
this.errorMap[element.name] = message;
|
|
1331
1887
|
this.submitted[element.name] = message;
|
|
@@ -1359,7 +1915,6 @@ class Validator {
|
|
|
1359
1915
|
return result;
|
|
1360
1916
|
}
|
|
1361
1917
|
showLabel(element, message) {
|
|
1362
|
-
var _a;
|
|
1363
1918
|
let errors = this.errorsFor(element);
|
|
1364
1919
|
const elementID = idOrName(element);
|
|
1365
1920
|
const describedBy = element.getAttribute('aria-describedby');
|
|
@@ -1370,11 +1925,10 @@ class Validator {
|
|
|
1370
1925
|
.map((e) => e.closest(labelSelector))
|
|
1371
1926
|
.every((e) => e === null);
|
|
1372
1927
|
const noDescriberAssociation = () => {
|
|
1373
|
-
var _a, _b;
|
|
1374
1928
|
if (describedBy === null) {
|
|
1375
1929
|
return true;
|
|
1376
1930
|
}
|
|
1377
|
-
const firstErrorId =
|
|
1931
|
+
const firstErrorId = errors[0]?.getAttribute('id') ?? null;
|
|
1378
1932
|
if (firstErrorId === null) {
|
|
1379
1933
|
return true;
|
|
1380
1934
|
}
|
|
@@ -1440,7 +1994,7 @@ class Validator {
|
|
|
1440
1994
|
}
|
|
1441
1995
|
// If the element is not a child of an associated label, then it's necessary
|
|
1442
1996
|
// to explicitly apply aria-describedby
|
|
1443
|
-
else if (!
|
|
1997
|
+
else if (!newError.parentElement?.matches(`label[for='${escapeCssMeta(elementID)}']`)) {
|
|
1444
1998
|
this.addErrorAriaDescribedBy(element, newError.getAttribute('id'));
|
|
1445
1999
|
}
|
|
1446
2000
|
errors = [newError];
|
|
@@ -1484,10 +2038,27 @@ class Validator {
|
|
|
1484
2038
|
this.resetForm();
|
|
1485
2039
|
validatorStore.delete(this.currentForm);
|
|
1486
2040
|
this.eventManager.detachEventHandlers();
|
|
2041
|
+
if (this.addedNoValidate) {
|
|
2042
|
+
this.currentForm.removeAttribute('novalidate');
|
|
2043
|
+
}
|
|
1487
2044
|
}
|
|
1488
2045
|
resetForm() {
|
|
2046
|
+
// stop running async validations and forget their results
|
|
2047
|
+
Object.entries(this.pending).forEach(([name, { controller }]) => {
|
|
2048
|
+
controller.abort();
|
|
2049
|
+
if (this.settings.pendingClass) {
|
|
2050
|
+
findByName(this.currentForm, name).forEach((el) => el.classList.remove(this.settings.pendingClass));
|
|
2051
|
+
}
|
|
2052
|
+
});
|
|
2053
|
+
this.pending = {};
|
|
2054
|
+
this.pendingRequest = 0;
|
|
2055
|
+
this.asyncResults = {};
|
|
2056
|
+
this.formSubmitted = false;
|
|
2057
|
+
this.heldSubmitter = null;
|
|
2058
|
+
this.notifySettled();
|
|
1489
2059
|
this.invalid = {};
|
|
1490
2060
|
this.submitted = {};
|
|
2061
|
+
this.equalToChecked.clear();
|
|
1491
2062
|
this.prepareForm();
|
|
1492
2063
|
this.hideErrors();
|
|
1493
2064
|
this.elements().forEach((el) => el.removeAttribute('aria-invalid'));
|
|
@@ -1520,13 +2091,14 @@ class Validator {
|
|
|
1520
2091
|
* @return true if the element should be skipped, false otherwise
|
|
1521
2092
|
*/
|
|
1522
2093
|
shouldIgnore(element) {
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
2094
|
+
// built again only when the setting changes
|
|
2095
|
+
if (!this.ignoreTest || this.ignoreTest[0] !== this.settings.ignore) {
|
|
2096
|
+
this.ignoreTest = [
|
|
2097
|
+
this.settings.ignore,
|
|
2098
|
+
ignoreMatcher(this.settings.ignore),
|
|
2099
|
+
];
|
|
1528
2100
|
}
|
|
1529
|
-
return
|
|
2101
|
+
return this.ignoreTest[1](element);
|
|
1530
2102
|
}
|
|
1531
2103
|
getMessage(element, rule) {
|
|
1532
2104
|
if (typeof rule === 'string') {
|
|
@@ -1633,7 +2205,6 @@ function validate(selector, options) {
|
|
|
1633
2205
|
* }
|
|
1634
2206
|
*/
|
|
1635
2207
|
function valid(selector) {
|
|
1636
|
-
var _a, _b;
|
|
1637
2208
|
let elements = [];
|
|
1638
2209
|
if (typeof selector === 'string') {
|
|
1639
2210
|
elements = Array.from(document.querySelectorAll(selector));
|
|
@@ -1645,7 +2216,7 @@ function valid(selector) {
|
|
|
1645
2216
|
elements = selector;
|
|
1646
2217
|
}
|
|
1647
2218
|
if (elements[0].matches('form')) {
|
|
1648
|
-
return
|
|
2219
|
+
return validate(elements[0])?.form() ?? false;
|
|
1649
2220
|
}
|
|
1650
2221
|
const errorList = [];
|
|
1651
2222
|
let valid = true;
|
|
@@ -1662,6 +2233,37 @@ function valid(selector) {
|
|
|
1662
2233
|
validator.errorList = errorList;
|
|
1663
2234
|
return valid;
|
|
1664
2235
|
}
|
|
2236
|
+
/**
|
|
2237
|
+
* Like `dv.valid`, but waits for async validations (such as `remote`) to
|
|
2238
|
+
* finish, and resolves with the final result.
|
|
2239
|
+
*
|
|
2240
|
+
* @param selector - Element(s) to validate:
|
|
2241
|
+
* CSS selector, element, or array of elements
|
|
2242
|
+
* @returns Promise of true if all elements are valid, false otherwise
|
|
2243
|
+
* @example
|
|
2244
|
+
* if (await dv.validAsync('#signupForm')) {
|
|
2245
|
+
* await saveDraft();
|
|
2246
|
+
* }
|
|
2247
|
+
*/
|
|
2248
|
+
async function validAsync(selector) {
|
|
2249
|
+
let result = valid(selector);
|
|
2250
|
+
const first = typeof selector === 'string'
|
|
2251
|
+
? document.querySelector(selector)
|
|
2252
|
+
: Array.isArray(selector)
|
|
2253
|
+
? selector[0]
|
|
2254
|
+
: selector;
|
|
2255
|
+
const form = first instanceof HTMLFormElement
|
|
2256
|
+
? first
|
|
2257
|
+
: (first?.form ?? null);
|
|
2258
|
+
const validator = form ? validatorStore.get(form) : undefined;
|
|
2259
|
+
// wait for the answers, then validate again with them; repeat if a field
|
|
2260
|
+
// changed meanwhile and started a new request
|
|
2261
|
+
while (validator?.pendingRequest) {
|
|
2262
|
+
await validator.settled();
|
|
2263
|
+
result = validator.withCachedResults(() => valid(selector));
|
|
2264
|
+
}
|
|
2265
|
+
return result;
|
|
2266
|
+
}
|
|
1665
2267
|
/**
|
|
1666
2268
|
* Gets the validation rules for an element.
|
|
1667
2269
|
*
|
|
@@ -1683,13 +2285,15 @@ function rules(selector) {
|
|
|
1683
2285
|
return getRules(element, validator.rules);
|
|
1684
2286
|
}
|
|
1685
2287
|
/**
|
|
1686
|
-
* Adds a custom validation method
|
|
2288
|
+
* Adds a custom validation method, or replaces an existing one (as jQuery's
|
|
2289
|
+
* `$.validator.addMethod` does, e.g. `number` for a culture).
|
|
1687
2290
|
* The method can be used in validation rules by its name.
|
|
1688
2291
|
*
|
|
1689
2292
|
* @param name - Name of the validation method
|
|
1690
2293
|
* @param method - Validation function that returns true if valid
|
|
1691
|
-
* @param message - Default error message for this validation method
|
|
1692
|
-
*
|
|
2294
|
+
* @param message - Default error message for this validation method; when
|
|
2295
|
+
* omitted, an existing method keeps its message
|
|
2296
|
+
* @param addAsClassRule - Whether to allow using this method as a CSS class (default: false). Set to true for methods that take no param.
|
|
1693
2297
|
* @example
|
|
1694
2298
|
* // Add a custom phone validation method
|
|
1695
2299
|
* dv.addMethod('phone', function(blank, value, element) {
|
|
@@ -1709,12 +2313,10 @@ function addMethod(name, method, message, addAsClassRule = false) {
|
|
|
1709
2313
|
console.warn('Invalid method name: must be non-empty and only alphanumeric characters.');
|
|
1710
2314
|
return;
|
|
1711
2315
|
}
|
|
1712
|
-
if (store$1.get(name)) {
|
|
1713
|
-
console.warn(`Invalid method name: ${name} already exists.`);
|
|
1714
|
-
return;
|
|
1715
|
-
}
|
|
1716
2316
|
store$1.set(name, method);
|
|
1717
|
-
|
|
2317
|
+
if (message !== undefined) {
|
|
2318
|
+
store.set(name, message);
|
|
2319
|
+
}
|
|
1718
2320
|
if (addAsClassRule) {
|
|
1719
2321
|
addClassRule(name);
|
|
1720
2322
|
}
|
|
@@ -1750,6 +2352,7 @@ function localize(data) {
|
|
|
1750
2352
|
var index = {
|
|
1751
2353
|
validate,
|
|
1752
2354
|
valid,
|
|
2355
|
+
validAsync,
|
|
1753
2356
|
rules,
|
|
1754
2357
|
addMethod,
|
|
1755
2358
|
localize,
|
|
@@ -1757,5 +2360,5 @@ var index = {
|
|
|
1757
2360
|
methods: store$1,
|
|
1758
2361
|
};
|
|
1759
2362
|
|
|
1760
|
-
export { addMethod, index as default, localize, store as messages, store$1 as methods, rules, valid, validate };
|
|
2363
|
+
export { addMethod, index as default, localize, store as messages, store$1 as methods, rules, valid, validAsync, validate };
|
|
1761
2364
|
//# sourceMappingURL=dv.esm.js.map
|