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.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);
|
|
@@ -253,20 +373,23 @@ const store$1 = new ObjectStore({
|
|
|
253
373
|
email,
|
|
254
374
|
url,
|
|
255
375
|
date,
|
|
256
|
-
dateISO,
|
|
257
376
|
number,
|
|
258
377
|
digits,
|
|
259
|
-
equalTo,
|
|
378
|
+
equalto: equalTo,
|
|
260
379
|
regex,
|
|
261
380
|
nonalphamin: nonAlphaMin,
|
|
262
381
|
creditcard: creditCard,
|
|
382
|
+
extension,
|
|
383
|
+
remote,
|
|
263
384
|
});
|
|
264
385
|
/**
|
|
265
|
-
* Validates that the field has a value (is not blank).
|
|
266
|
-
*
|
|
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
|
|
267
390
|
*/
|
|
268
|
-
function required({ blank }) {
|
|
269
|
-
return !blank;
|
|
391
|
+
function required({ blank, param, element }) {
|
|
392
|
+
return !blank || !depend(param, element);
|
|
270
393
|
}
|
|
271
394
|
/**
|
|
272
395
|
* Validates that the field value has at least the minimum length.
|
|
@@ -339,25 +462,13 @@ function url({ blank, value }) {
|
|
|
339
462
|
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;
|
|
340
463
|
return re.test(value);
|
|
341
464
|
}
|
|
342
|
-
/**
|
|
343
|
-
* Validates that the field value is a valid date string
|
|
344
|
-
* @param blank - whether the field is blank
|
|
345
|
-
* @param value - field value
|
|
346
|
-
* @return true if blank or valid date string
|
|
347
|
-
*/
|
|
348
|
-
function date({ blank, value }) {
|
|
349
|
-
if (blank) {
|
|
350
|
-
return true;
|
|
351
|
-
}
|
|
352
|
-
return !/Invalid|NaN/.test(new Date(value).toString());
|
|
353
|
-
}
|
|
354
465
|
/**
|
|
355
466
|
* Validates that the field value is a valid ISO date format (YYYY-MM-DD or YYYY/MM/DD).
|
|
356
467
|
* @param blank - whether the field is blank
|
|
357
468
|
* @param value - field value
|
|
358
469
|
* @return true if blank or valid ISO date format
|
|
359
470
|
*/
|
|
360
|
-
function
|
|
471
|
+
function date({ blank, value }) {
|
|
361
472
|
if (blank) {
|
|
362
473
|
return true;
|
|
363
474
|
}
|
|
@@ -394,8 +505,8 @@ function digits({ blank, value }) {
|
|
|
394
505
|
* @return true if values match
|
|
395
506
|
*/
|
|
396
507
|
function equalTo({ value, param }) {
|
|
397
|
-
const target = document.querySelector(param);
|
|
398
|
-
return value === target.value;
|
|
508
|
+
const target = param instanceof Element ? param : document.querySelector(param);
|
|
509
|
+
return !!target && value === target.value;
|
|
399
510
|
}
|
|
400
511
|
/**
|
|
401
512
|
* Validates that the field value matches a regular expression.
|
|
@@ -429,6 +540,10 @@ function creditCard({ blank, value }) {
|
|
|
429
540
|
if (blank) {
|
|
430
541
|
return true;
|
|
431
542
|
}
|
|
543
|
+
// only digits, spaces and dashes are allowed
|
|
544
|
+
if (/[^0-9 \-]+/.test(value)) {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
432
547
|
const strippedValue = value.replace(/\D/g, '');
|
|
433
548
|
let nCheck = 0;
|
|
434
549
|
let bEven = false;
|
|
@@ -450,6 +565,77 @@ function creditCard({ blank, value }) {
|
|
|
450
565
|
}
|
|
451
566
|
return nCheck % 10 === 0;
|
|
452
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
|
+
}
|
|
453
639
|
|
|
454
640
|
/**
|
|
455
641
|
* Gets all validation rules for an element from multiple sources.
|
|
@@ -463,12 +649,23 @@ function getRules(element, settings) {
|
|
|
463
649
|
if (element == null || element.form == null) {
|
|
464
650
|
return {};
|
|
465
651
|
}
|
|
466
|
-
let data = normalizeRules(
|
|
652
|
+
let data = normalizeRules({
|
|
653
|
+
...classRules(element),
|
|
654
|
+
...attributeRules(element),
|
|
655
|
+
...dataRules(element),
|
|
656
|
+
...staticRules(element, settings),
|
|
657
|
+
}, element);
|
|
467
658
|
// Make sure required is at front
|
|
468
|
-
if (data
|
|
659
|
+
if ('required' in data) {
|
|
469
660
|
const param = data.required;
|
|
470
661
|
delete data.required;
|
|
471
|
-
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;
|
|
472
669
|
}
|
|
473
670
|
return data;
|
|
474
671
|
}
|
|
@@ -481,7 +678,6 @@ const classRuleSettings = {
|
|
|
481
678
|
email: { email: true },
|
|
482
679
|
url: { url: true },
|
|
483
680
|
date: { date: true },
|
|
484
|
-
dateISO: { dateISO: true },
|
|
485
681
|
number: { number: true },
|
|
486
682
|
digits: { digits: true },
|
|
487
683
|
creditcard: { creditcard: true },
|
|
@@ -495,19 +691,37 @@ function addClassRule(className) {
|
|
|
495
691
|
}
|
|
496
692
|
/**
|
|
497
693
|
* Normalizes and processes validation rules for an element.
|
|
498
|
-
* Removes false rules,
|
|
694
|
+
* Removes false rules, applies conditional rules (`{ param, depends }`),
|
|
695
|
+
* evaluates function parameters, and converts string/number values.
|
|
499
696
|
* @param rules - raw validation rules object
|
|
697
|
+
* @param element - the element the rules are for
|
|
500
698
|
* @return normalized rules object
|
|
501
699
|
*/
|
|
502
|
-
function normalizeRules(rules) {
|
|
700
|
+
function normalizeRules(rules, element) {
|
|
503
701
|
Object.entries(rules).forEach(([key, value]) => {
|
|
504
|
-
// Ignore rule when param is explicitly false, eg. required:false
|
|
505
|
-
|
|
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) {
|
|
506
705
|
delete rules[key];
|
|
507
706
|
return;
|
|
508
707
|
}
|
|
509
|
-
|
|
510
|
-
|
|
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);
|
|
511
725
|
}
|
|
512
726
|
});
|
|
513
727
|
// Clean number parameters
|
|
@@ -541,9 +755,13 @@ function classRules(element) {
|
|
|
541
755
|
let rules = {};
|
|
542
756
|
const classes = element.getAttribute('class');
|
|
543
757
|
if (classes) {
|
|
544
|
-
classes
|
|
758
|
+
classes
|
|
759
|
+
.trim()
|
|
760
|
+
.split(/\s/)
|
|
761
|
+
.map((className) => className.toLowerCase())
|
|
762
|
+
.forEach((className) => {
|
|
545
763
|
if (className in classRuleSettings) {
|
|
546
|
-
rules =
|
|
764
|
+
rules = { ...rules, ...classRuleSettings[className] };
|
|
547
765
|
}
|
|
548
766
|
});
|
|
549
767
|
}
|
|
@@ -584,14 +802,13 @@ function attributeRules(element) {
|
|
|
584
802
|
* @return validation rules derived from data attributes
|
|
585
803
|
*/
|
|
586
804
|
function dataRules(element) {
|
|
587
|
-
var _a;
|
|
588
805
|
const rules = {};
|
|
589
806
|
const type = element.type;
|
|
590
807
|
for (const method of store$1.keys()) {
|
|
591
808
|
const datasetKey = 'rule' +
|
|
592
809
|
method.charAt(0).toUpperCase() +
|
|
593
810
|
method.substring(1).toLowerCase();
|
|
594
|
-
let value =
|
|
811
|
+
let value = element.dataset[datasetKey] ?? null;
|
|
595
812
|
// Cast empty attributes like `data-rule-required` to `true`
|
|
596
813
|
if (value === '' || value === 'true') {
|
|
597
814
|
value = true;
|
|
@@ -637,7 +854,7 @@ function normalizeAttributeRule(rules, type, method, value) {
|
|
|
637
854
|
else if (type === method && type !== 'range') {
|
|
638
855
|
// Exception: the jquery validate 'range' method
|
|
639
856
|
// does not test for the html5 'range' type
|
|
640
|
-
rules[
|
|
857
|
+
rules[method] = true;
|
|
641
858
|
}
|
|
642
859
|
}
|
|
643
860
|
/**
|
|
@@ -648,10 +865,14 @@ function normalizeAttributeRule(rules, type, method, value) {
|
|
|
648
865
|
*/
|
|
649
866
|
function normalizeRule(data) {
|
|
650
867
|
if (typeof data !== 'string') {
|
|
651
|
-
return data;
|
|
868
|
+
return Object.fromEntries(Object.entries(data).map(([key, value]) => [key.toLowerCase(), value]));
|
|
652
869
|
}
|
|
653
870
|
const transformed = {};
|
|
654
|
-
data
|
|
871
|
+
data
|
|
872
|
+
.trim()
|
|
873
|
+
.split(/\s/)
|
|
874
|
+
.map((token) => token.toLowerCase())
|
|
875
|
+
.forEach((token) => {
|
|
655
876
|
transformed[token] = true;
|
|
656
877
|
});
|
|
657
878
|
return transformed;
|
|
@@ -665,10 +886,9 @@ const store = new ObjectStore({
|
|
|
665
886
|
email: 'Please enter a valid email address.',
|
|
666
887
|
url: 'Please enter a valid URL.',
|
|
667
888
|
date: 'Please enter a valid date.',
|
|
668
|
-
dateISO: 'Please enter a valid date (ISO).',
|
|
669
889
|
number: 'Please enter a valid number.',
|
|
670
890
|
digits: 'Please enter only digits.',
|
|
671
|
-
|
|
891
|
+
equalto: 'Please enter the same value again.',
|
|
672
892
|
maxlength: 'Please enter no more than {0} characters.',
|
|
673
893
|
minlength: 'Please enter at least {0} characters.',
|
|
674
894
|
rangelength: 'Please enter a value between {0} and {1} characters long.',
|
|
@@ -678,6 +898,8 @@ const store = new ObjectStore({
|
|
|
678
898
|
regex: 'Please enter a value that matches the pattern {0}.',
|
|
679
899
|
nonalphamin: 'Please enter at least {0} non-alphabetic characters.',
|
|
680
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.',
|
|
681
903
|
});
|
|
682
904
|
/**
|
|
683
905
|
* Gets the appropriate error message for a validation rule failure.
|
|
@@ -689,13 +911,17 @@ const store = new ObjectStore({
|
|
|
689
911
|
* @return formatted error message
|
|
690
912
|
*/
|
|
691
913
|
function getMessage(element, rule, ignoreTitle, customMessage) {
|
|
692
|
-
|
|
914
|
+
const message = [
|
|
693
915
|
customMessage,
|
|
694
916
|
customDataMessage(element, rule.method),
|
|
695
917
|
(!ignoreTitle && element.title) || undefined,
|
|
696
918
|
store.get(rule.method),
|
|
697
919
|
`<strong>Warning: No message defined for ${element.name}</strong>`,
|
|
698
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
|
+
}
|
|
699
925
|
const regex = /\$?\{(\d+)}/g;
|
|
700
926
|
if (/\$?\{(\d+)}/g.test(message)) {
|
|
701
927
|
return format(message.replace(regex, '{$1}'), rule.parameters);
|
|
@@ -713,7 +939,7 @@ function getMessage(element, rule, ignoreTitle, customMessage) {
|
|
|
713
939
|
*/
|
|
714
940
|
function customDataMessage(element, method) {
|
|
715
941
|
const dataSetKey = 'msg' + method.charAt(0).toUpperCase() + method.substring(1).toLowerCase();
|
|
716
|
-
if (!
|
|
942
|
+
if (!element?.dataset) {
|
|
717
943
|
return;
|
|
718
944
|
}
|
|
719
945
|
return element.dataset[dataSetKey] || element.dataset['msg'];
|
|
@@ -727,6 +953,8 @@ const validatorStore = new WeakMap();
|
|
|
727
953
|
const EVENT_TARGETS = {
|
|
728
954
|
FOCUS_TARGETS: [
|
|
729
955
|
"[type='text']",
|
|
956
|
+
// an <input> without a type is a text input
|
|
957
|
+
'input:not([type])',
|
|
730
958
|
"[type='password']",
|
|
731
959
|
"[type='file']",
|
|
732
960
|
'select',
|
|
@@ -756,18 +984,24 @@ const EVENT_TARGETS = {
|
|
|
756
984
|
* Handles attaching and removing event listeners with proper delegation logic.
|
|
757
985
|
*/
|
|
758
986
|
class FormEventManager {
|
|
759
|
-
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) {
|
|
760
990
|
this.form = form;
|
|
761
991
|
this.settings = settings;
|
|
762
992
|
this.validateCallback = validateCallback;
|
|
763
993
|
this.focusInvalidCallback = focusInvalidCallback;
|
|
764
994
|
this.shouldIgnoreElement = shouldIgnoreElement;
|
|
995
|
+
this.holdSubmitCallback = holdSubmitCallback;
|
|
765
996
|
this.boundEventHandlers = {
|
|
766
997
|
onFocusIn: null,
|
|
767
998
|
onFocusOut: null,
|
|
768
999
|
onKeyUp: null,
|
|
769
1000
|
onClick: null,
|
|
1001
|
+
onSubmit: null,
|
|
1002
|
+
onInvalidForm: null,
|
|
770
1003
|
};
|
|
1004
|
+
this.submitButton = null;
|
|
771
1005
|
}
|
|
772
1006
|
/**
|
|
773
1007
|
* Attaches all validation event listeners to the form
|
|
@@ -782,16 +1016,18 @@ class FormEventManager {
|
|
|
782
1016
|
this.form.addEventListener('keyup', this.boundEventHandlers.onKeyUp);
|
|
783
1017
|
this.form.addEventListener('click', this.boundEventHandlers.onClick);
|
|
784
1018
|
if (this.settings.onsubmit) {
|
|
785
|
-
this.
|
|
1019
|
+
this.boundEventHandlers.onSubmit = (e) => this.handleSubmitForm(e);
|
|
1020
|
+
this.form.addEventListener('submit', this.boundEventHandlers.onSubmit);
|
|
786
1021
|
}
|
|
1022
|
+
// the validator wraps it to pass itself as the second argument
|
|
787
1023
|
const invalidHandler = this.settings.invalidHandler;
|
|
788
1024
|
if (invalidHandler) {
|
|
789
|
-
this.
|
|
790
|
-
if (
|
|
791
|
-
|
|
1025
|
+
this.boundEventHandlers.onInvalidForm = (e) => {
|
|
1026
|
+
if (this.isCustomEvent(e)) {
|
|
1027
|
+
invalidHandler(e);
|
|
792
1028
|
}
|
|
793
|
-
|
|
794
|
-
|
|
1029
|
+
};
|
|
1030
|
+
this.form.addEventListener('invalid-form', this.boundEventHandlers.onInvalidForm);
|
|
795
1031
|
}
|
|
796
1032
|
}
|
|
797
1033
|
triggerInvalidForm(errorList) {
|
|
@@ -808,11 +1044,54 @@ class FormEventManager {
|
|
|
808
1044
|
if (this.settings.debug) {
|
|
809
1045
|
e.preventDefault();
|
|
810
1046
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
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
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
let hidden = null;
|
|
1076
|
+
// Insert a hidden input as a replacement for the missing submit button
|
|
1077
|
+
// if a submitHandler is passed as option
|
|
1078
|
+
if (this.submitButton?.name && this.settings.submitHandler) {
|
|
1079
|
+
hidden = document.createElement('input');
|
|
1080
|
+
hidden.setAttribute('type', 'hidden');
|
|
1081
|
+
hidden.setAttribute('name', this.submitButton.name);
|
|
1082
|
+
hidden.setAttribute('value', this.submitButton.value);
|
|
1083
|
+
this.form.appendChild(hidden);
|
|
1084
|
+
}
|
|
1085
|
+
if (this.settings.submitHandler && !this.settings.debug) {
|
|
1086
|
+
const result = this.settings.submitHandler(this.form, e);
|
|
1087
|
+
if (hidden) {
|
|
1088
|
+
// And clean up afterwards
|
|
1089
|
+
hidden.remove();
|
|
1090
|
+
}
|
|
1091
|
+
if (!result) {
|
|
1092
|
+
e.preventDefault();
|
|
1093
|
+
e.stopPropagation();
|
|
1094
|
+
}
|
|
816
1095
|
}
|
|
817
1096
|
}
|
|
818
1097
|
/**
|
|
@@ -831,11 +1110,19 @@ class FormEventManager {
|
|
|
831
1110
|
if (this.boundEventHandlers.onClick) {
|
|
832
1111
|
this.form.removeEventListener('click', this.boundEventHandlers.onClick);
|
|
833
1112
|
}
|
|
1113
|
+
if (this.boundEventHandlers.onSubmit) {
|
|
1114
|
+
this.form.removeEventListener('submit', this.boundEventHandlers.onSubmit);
|
|
1115
|
+
}
|
|
1116
|
+
if (this.boundEventHandlers.onInvalidForm) {
|
|
1117
|
+
this.form.removeEventListener('invalid-form', this.boundEventHandlers.onInvalidForm);
|
|
1118
|
+
}
|
|
834
1119
|
// Clear references
|
|
835
1120
|
this.boundEventHandlers.onFocusIn = null;
|
|
836
1121
|
this.boundEventHandlers.onFocusOut = null;
|
|
837
1122
|
this.boundEventHandlers.onKeyUp = null;
|
|
838
1123
|
this.boundEventHandlers.onClick = null;
|
|
1124
|
+
this.boundEventHandlers.onSubmit = null;
|
|
1125
|
+
this.boundEventHandlers.onInvalidForm = null;
|
|
839
1126
|
}
|
|
840
1127
|
/**
|
|
841
1128
|
* Creates a delegated event handler for the specified targets and handler
|
|
@@ -883,6 +1170,7 @@ const ValidatorDefaults = {
|
|
|
883
1170
|
ignore: ':hidden',
|
|
884
1171
|
errorClass: 'error',
|
|
885
1172
|
validClass: 'valid',
|
|
1173
|
+
pendingClass: 'pending',
|
|
886
1174
|
errorElement: 'label',
|
|
887
1175
|
wrapper: null,
|
|
888
1176
|
errorLabelContainer: null,
|
|
@@ -904,11 +1192,12 @@ const ValidatorDefaults = {
|
|
|
904
1192
|
onsubmit: true,
|
|
905
1193
|
debug: false,
|
|
906
1194
|
invalidHandler: null,
|
|
1195
|
+
submitHandler: null,
|
|
907
1196
|
};
|
|
908
1197
|
function onFocusIn(element) {
|
|
909
1198
|
this.lastActive = element;
|
|
910
1199
|
if (this.settings.focusCleanup) {
|
|
911
|
-
if (
|
|
1200
|
+
if (this.settings.unhighlight) {
|
|
912
1201
|
this.settings.unhighlight(element, this.errorClasses, this.validClasses);
|
|
913
1202
|
}
|
|
914
1203
|
this.hideErrors(element);
|
|
@@ -987,6 +1276,40 @@ function unhighlight(element, errorClasses, validClasses) {
|
|
|
987
1276
|
});
|
|
988
1277
|
}
|
|
989
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
|
+
]);
|
|
990
1313
|
class Validator {
|
|
991
1314
|
/**
|
|
992
1315
|
* Initializes a new validator instance for the given form with optional configuration settings.
|
|
@@ -995,6 +1318,7 @@ class Validator {
|
|
|
995
1318
|
*/
|
|
996
1319
|
constructor(form, options) {
|
|
997
1320
|
this.submitted = {};
|
|
1321
|
+
/** fields validated so far: their message, or false when valid */
|
|
998
1322
|
this.invalid = {};
|
|
999
1323
|
this.successList = [];
|
|
1000
1324
|
this.errorList = [];
|
|
@@ -1007,9 +1331,27 @@ class Validator {
|
|
|
1007
1331
|
this.lastActive = null;
|
|
1008
1332
|
/** normalized rules */
|
|
1009
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 = [];
|
|
1010
1352
|
this.settings = ValidatorDefaults;
|
|
1011
1353
|
this.currentForm = form;
|
|
1012
|
-
this.settings =
|
|
1354
|
+
this.settings = { ...this.settings, ...options };
|
|
1013
1355
|
this.normalizeSettings();
|
|
1014
1356
|
this.init();
|
|
1015
1357
|
}
|
|
@@ -1027,34 +1369,103 @@ class Validator {
|
|
|
1027
1369
|
'highlight',
|
|
1028
1370
|
'unhighlight',
|
|
1029
1371
|
'errorPlacement',
|
|
1030
|
-
'
|
|
1372
|
+
'submitHandler',
|
|
1031
1373
|
].forEach((key) => {
|
|
1032
1374
|
const setting = this.settings[key];
|
|
1033
1375
|
if (setting && typeof setting === 'function') {
|
|
1034
1376
|
this.settings[key] = setting.bind(this);
|
|
1035
1377
|
}
|
|
1036
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 };
|
|
1387
|
+
// transform method names to lowercase in messages
|
|
1388
|
+
if (this.settings.messages) {
|
|
1389
|
+
Object.keys(this.settings.messages).forEach((elementName) => {
|
|
1390
|
+
const messages = this.settings.messages[elementName];
|
|
1391
|
+
// Only transform if it's an object (not a string)
|
|
1392
|
+
if (typeof messages === 'object' && messages !== null) {
|
|
1393
|
+
const normalizedMessages = {};
|
|
1394
|
+
Object.keys(messages).forEach((methodName) => {
|
|
1395
|
+
normalizedMessages[methodName.toLowerCase()] =
|
|
1396
|
+
messages[methodName];
|
|
1397
|
+
});
|
|
1398
|
+
this.settings.messages[elementName] = normalizedMessages;
|
|
1399
|
+
}
|
|
1400
|
+
});
|
|
1401
|
+
}
|
|
1037
1402
|
}
|
|
1038
1403
|
/**
|
|
1039
1404
|
* Sets up DOM references and attaches event handlers to the form.
|
|
1040
1405
|
*/
|
|
1041
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
|
+
}
|
|
1042
1413
|
if (this.settings.errorLabelContainer) {
|
|
1043
1414
|
this.labelContainer = document.querySelector(this.settings.errorLabelContainer);
|
|
1044
1415
|
}
|
|
1045
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
|
|
1046
1419
|
const containers = [this.labelContainer];
|
|
1047
1420
|
if (this.settings.errorContainer) {
|
|
1048
|
-
containers.push(document.
|
|
1421
|
+
containers.push(...document.querySelectorAll(this.settings.errorContainer));
|
|
1049
1422
|
}
|
|
1050
1423
|
this.containers = containers.filter(Boolean);
|
|
1051
1424
|
this.rules = {};
|
|
1052
1425
|
Object.entries(this.settings.rules).forEach(([key, value]) => {
|
|
1053
1426
|
this.rules[key] = normalizeRule(value);
|
|
1054
1427
|
});
|
|
1055
|
-
|
|
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));
|
|
1056
1437
|
this.eventManager.attachEventHandlers();
|
|
1057
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
|
+
}
|
|
1058
1469
|
/**
|
|
1059
1470
|
* Resets the validator state.
|
|
1060
1471
|
*/
|
|
@@ -1071,17 +1482,19 @@ class Validator {
|
|
|
1071
1482
|
* @return true if the form is valid, false otherwise
|
|
1072
1483
|
*/
|
|
1073
1484
|
form() {
|
|
1485
|
+
this.retryFailedRequests();
|
|
1074
1486
|
this.prepareForm();
|
|
1075
1487
|
this.currentElements = this.elements();
|
|
1076
1488
|
for (const element of this.currentElements) {
|
|
1077
1489
|
this.check(element);
|
|
1078
1490
|
}
|
|
1079
|
-
this.submitted =
|
|
1080
|
-
this.invalid =
|
|
1491
|
+
this.submitted = { ...this.submitted, ...this.errorMap };
|
|
1492
|
+
this.invalid = { ...this.errorMap };
|
|
1081
1493
|
if (!this.valid()) {
|
|
1082
1494
|
this.eventManager.triggerInvalidForm(this.errorList);
|
|
1083
1495
|
}
|
|
1084
1496
|
this.showErrors();
|
|
1497
|
+
this.notifySettled();
|
|
1085
1498
|
return this.valid();
|
|
1086
1499
|
}
|
|
1087
1500
|
/**
|
|
@@ -1090,6 +1503,7 @@ class Validator {
|
|
|
1090
1503
|
* @return true if the element is valid, false otherwise
|
|
1091
1504
|
*/
|
|
1092
1505
|
element(element) {
|
|
1506
|
+
this.retryFailedRequests();
|
|
1093
1507
|
const target = this.validationTargetFor(element);
|
|
1094
1508
|
if (target === undefined) {
|
|
1095
1509
|
delete this.invalid[element.name];
|
|
@@ -1098,18 +1512,16 @@ class Validator {
|
|
|
1098
1512
|
this.prepareElement(target);
|
|
1099
1513
|
this.currentElements = [target];
|
|
1100
1514
|
const result = this.check(target);
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
else {
|
|
1105
|
-
this.invalid[target.name] = 'Invalid';
|
|
1106
|
-
}
|
|
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';
|
|
1107
1518
|
if (!this.numberOfInvalids()) {
|
|
1108
1519
|
this.toHide.push(...this.containers);
|
|
1109
1520
|
}
|
|
1110
1521
|
this.showErrors();
|
|
1111
1522
|
// add aria-invalid status for screen readers
|
|
1112
1523
|
element.setAttribute('aria-invalid', (!result).toString());
|
|
1524
|
+
this.notifySettled();
|
|
1113
1525
|
return result;
|
|
1114
1526
|
}
|
|
1115
1527
|
prepareElement(element) {
|
|
@@ -1137,9 +1549,11 @@ class Validator {
|
|
|
1137
1549
|
elements() {
|
|
1138
1550
|
const rulesCache = {};
|
|
1139
1551
|
const selector = 'input, select, textarea';
|
|
1140
|
-
|
|
1552
|
+
// :disabled also matches fields inside <fieldset disabled>
|
|
1553
|
+
const notSelector = 'input[type="submit"], [type="reset"], [type="image"], :disabled';
|
|
1141
1554
|
const elements = Array.from(this.currentForm.querySelectorAll(selector)).filter((el) => !el.matches(notSelector) && !this.shouldIgnore(el));
|
|
1142
|
-
|
|
1555
|
+
// getAttribute: a field named "id" shadows form.id
|
|
1556
|
+
const formId = this.currentForm.getAttribute('id');
|
|
1143
1557
|
if (formId) {
|
|
1144
1558
|
const escapedId = escapeCssMeta(formId);
|
|
1145
1559
|
const outsideElements = Array.from(document.querySelectorAll(selector)).filter((el) => el.matches(`[form="${escapedId}"]`) &&
|
|
@@ -1168,7 +1582,8 @@ class Validator {
|
|
|
1168
1582
|
*/
|
|
1169
1583
|
validElements() {
|
|
1170
1584
|
const invalid = this.invalidElements();
|
|
1171
|
-
|
|
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));
|
|
1172
1587
|
}
|
|
1173
1588
|
/**
|
|
1174
1589
|
* Returns an array of currently invalid elements from the last validation.
|
|
@@ -1183,14 +1598,24 @@ class Validator {
|
|
|
1183
1598
|
* @return true if the element is valid, false otherwise
|
|
1184
1599
|
*/
|
|
1185
1600
|
check(element) {
|
|
1186
|
-
var _a;
|
|
1187
1601
|
element = this.validationTargetFor(element);
|
|
1188
1602
|
const rules = getRules(element, this.rules);
|
|
1189
1603
|
const elValue = elementValue(element, this.currentForm);
|
|
1190
|
-
const value = (
|
|
1604
|
+
const value = (Array.isArray(elValue) ? elValue[0] : elValue) ?? '';
|
|
1191
1605
|
const values = Array.isArray(elValue) ? elValue : [elValue];
|
|
1192
1606
|
const length = getValueLength(element, this.currentForm);
|
|
1193
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;
|
|
1194
1619
|
for (const method in rules) {
|
|
1195
1620
|
const rule = { method, parameters: rules[method] };
|
|
1196
1621
|
const methodFunc = store$1.get(method);
|
|
@@ -1198,7 +1623,25 @@ class Validator {
|
|
|
1198
1623
|
console.warn(`Validation method "${method}" not found. Skipping.`);
|
|
1199
1624
|
continue;
|
|
1200
1625
|
}
|
|
1201
|
-
|
|
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();
|
|
1202
1645
|
const result = methodFunc({
|
|
1203
1646
|
blank,
|
|
1204
1647
|
value: value,
|
|
@@ -1206,21 +1649,189 @@ class Validator {
|
|
|
1206
1649
|
length,
|
|
1207
1650
|
element,
|
|
1208
1651
|
param: rule.parameters,
|
|
1652
|
+
signal: controller.signal,
|
|
1209
1653
|
});
|
|
1210
|
-
if (!result) {
|
|
1211
|
-
|
|
1212
|
-
|
|
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;
|
|
1213
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));
|
|
1214
1669
|
}
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
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;
|
|
1218
1675
|
}
|
|
1219
|
-
if (objectLength(rules)) {
|
|
1676
|
+
if (!neutral && objectLength(rules)) {
|
|
1220
1677
|
this.successList.push(element);
|
|
1221
1678
|
}
|
|
1222
1679
|
return true;
|
|
1223
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
|
+
}
|
|
1224
1835
|
/**
|
|
1225
1836
|
* Returns all error label elements currently in the DOM.
|
|
1226
1837
|
* @return array of error label elements
|
|
@@ -1232,7 +1843,7 @@ class Validator {
|
|
|
1232
1843
|
showErrors(errors) {
|
|
1233
1844
|
if (errors) {
|
|
1234
1845
|
// Add items to error list and map
|
|
1235
|
-
this.errorMap =
|
|
1846
|
+
this.errorMap = { ...this.errorMap, ...errors };
|
|
1236
1847
|
this.errorList = Object.entries(this.errorMap).map(([name, message]) => ({
|
|
1237
1848
|
message,
|
|
1238
1849
|
method: message,
|
|
@@ -1270,8 +1881,7 @@ class Validator {
|
|
|
1270
1881
|
this.hideErrors();
|
|
1271
1882
|
this.addWrapper(...this.toShow).forEach(showElement);
|
|
1272
1883
|
}
|
|
1273
|
-
formatAndAdd(element, rule) {
|
|
1274
|
-
const message = this.getMessage(element, rule);
|
|
1884
|
+
formatAndAdd(element, rule, message = this.getMessage(element, rule)) {
|
|
1275
1885
|
this.errorList.push({ message, element, method: rule.method });
|
|
1276
1886
|
this.errorMap[element.name] = message;
|
|
1277
1887
|
this.submitted[element.name] = message;
|
|
@@ -1305,7 +1915,6 @@ class Validator {
|
|
|
1305
1915
|
return result;
|
|
1306
1916
|
}
|
|
1307
1917
|
showLabel(element, message) {
|
|
1308
|
-
var _a;
|
|
1309
1918
|
let errors = this.errorsFor(element);
|
|
1310
1919
|
const elementID = idOrName(element);
|
|
1311
1920
|
const describedBy = element.getAttribute('aria-describedby');
|
|
@@ -1316,11 +1925,10 @@ class Validator {
|
|
|
1316
1925
|
.map((e) => e.closest(labelSelector))
|
|
1317
1926
|
.every((e) => e === null);
|
|
1318
1927
|
const noDescriberAssociation = () => {
|
|
1319
|
-
var _a, _b;
|
|
1320
1928
|
if (describedBy === null) {
|
|
1321
1929
|
return true;
|
|
1322
1930
|
}
|
|
1323
|
-
const firstErrorId =
|
|
1931
|
+
const firstErrorId = errors[0]?.getAttribute('id') ?? null;
|
|
1324
1932
|
if (firstErrorId === null) {
|
|
1325
1933
|
return true;
|
|
1326
1934
|
}
|
|
@@ -1386,7 +1994,7 @@ class Validator {
|
|
|
1386
1994
|
}
|
|
1387
1995
|
// If the element is not a child of an associated label, then it's necessary
|
|
1388
1996
|
// to explicitly apply aria-describedby
|
|
1389
|
-
else if (!
|
|
1997
|
+
else if (!newError.parentElement?.matches(`label[for='${escapeCssMeta(elementID)}']`)) {
|
|
1390
1998
|
this.addErrorAriaDescribedBy(element, newError.getAttribute('id'));
|
|
1391
1999
|
}
|
|
1392
2000
|
errors = [newError];
|
|
@@ -1430,10 +2038,27 @@ class Validator {
|
|
|
1430
2038
|
this.resetForm();
|
|
1431
2039
|
validatorStore.delete(this.currentForm);
|
|
1432
2040
|
this.eventManager.detachEventHandlers();
|
|
2041
|
+
if (this.addedNoValidate) {
|
|
2042
|
+
this.currentForm.removeAttribute('novalidate');
|
|
2043
|
+
}
|
|
1433
2044
|
}
|
|
1434
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();
|
|
1435
2059
|
this.invalid = {};
|
|
1436
2060
|
this.submitted = {};
|
|
2061
|
+
this.equalToChecked.clear();
|
|
1437
2062
|
this.prepareForm();
|
|
1438
2063
|
this.hideErrors();
|
|
1439
2064
|
this.elements().forEach((el) => el.removeAttribute('aria-invalid'));
|
|
@@ -1466,13 +2091,14 @@ class Validator {
|
|
|
1466
2091
|
* @return true if the element should be skipped, false otherwise
|
|
1467
2092
|
*/
|
|
1468
2093
|
shouldIgnore(element) {
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
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
|
+
];
|
|
1474
2100
|
}
|
|
1475
|
-
return
|
|
2101
|
+
return this.ignoreTest[1](element);
|
|
1476
2102
|
}
|
|
1477
2103
|
getMessage(element, rule) {
|
|
1478
2104
|
if (typeof rule === 'string') {
|
|
@@ -1579,7 +2205,6 @@ function validate(selector, options) {
|
|
|
1579
2205
|
* }
|
|
1580
2206
|
*/
|
|
1581
2207
|
function valid(selector) {
|
|
1582
|
-
var _a, _b;
|
|
1583
2208
|
let elements = [];
|
|
1584
2209
|
if (typeof selector === 'string') {
|
|
1585
2210
|
elements = Array.from(document.querySelectorAll(selector));
|
|
@@ -1591,7 +2216,7 @@ function valid(selector) {
|
|
|
1591
2216
|
elements = selector;
|
|
1592
2217
|
}
|
|
1593
2218
|
if (elements[0].matches('form')) {
|
|
1594
|
-
return
|
|
2219
|
+
return validate(elements[0])?.form() ?? false;
|
|
1595
2220
|
}
|
|
1596
2221
|
const errorList = [];
|
|
1597
2222
|
let valid = true;
|
|
@@ -1608,6 +2233,37 @@ function valid(selector) {
|
|
|
1608
2233
|
validator.errorList = errorList;
|
|
1609
2234
|
return valid;
|
|
1610
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
|
+
}
|
|
1611
2267
|
/**
|
|
1612
2268
|
* Gets the validation rules for an element.
|
|
1613
2269
|
*
|
|
@@ -1629,12 +2285,15 @@ function rules(selector) {
|
|
|
1629
2285
|
return getRules(element, validator.rules);
|
|
1630
2286
|
}
|
|
1631
2287
|
/**
|
|
1632
|
-
* 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).
|
|
1633
2290
|
* The method can be used in validation rules by its name.
|
|
1634
2291
|
*
|
|
1635
2292
|
* @param name - Name of the validation method
|
|
1636
2293
|
* @param method - Validation function that returns true if valid
|
|
1637
|
-
* @param message - Default error message for this validation method
|
|
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.
|
|
1638
2297
|
* @example
|
|
1639
2298
|
* // Add a custom phone validation method
|
|
1640
2299
|
* dv.addMethod('phone', function(blank, value, element) {
|
|
@@ -1648,12 +2307,17 @@ function rules(selector) {
|
|
|
1648
2307
|
* }
|
|
1649
2308
|
* });
|
|
1650
2309
|
*/
|
|
1651
|
-
function addMethod(name, method, message) {
|
|
2310
|
+
function addMethod(name, method, message, addAsClassRule = false) {
|
|
2311
|
+
name = name.trim().toLowerCase();
|
|
2312
|
+
if (name === '' || !/^[a-zA-Z0-9]+$/.test(name)) {
|
|
2313
|
+
console.warn('Invalid method name: must be non-empty and only alphanumeric characters.');
|
|
2314
|
+
return;
|
|
2315
|
+
}
|
|
1652
2316
|
store$1.set(name, method);
|
|
1653
|
-
if (message) {
|
|
2317
|
+
if (message !== undefined) {
|
|
1654
2318
|
store.set(name, message);
|
|
1655
2319
|
}
|
|
1656
|
-
if (
|
|
2320
|
+
if (addAsClassRule) {
|
|
1657
2321
|
addClassRule(name);
|
|
1658
2322
|
}
|
|
1659
2323
|
}
|
|
@@ -1688,6 +2352,7 @@ function localize(data) {
|
|
|
1688
2352
|
var index = {
|
|
1689
2353
|
validate,
|
|
1690
2354
|
valid,
|
|
2355
|
+
validAsync,
|
|
1691
2356
|
rules,
|
|
1692
2357
|
addMethod,
|
|
1693
2358
|
localize,
|
|
@@ -1695,5 +2360,5 @@ var index = {
|
|
|
1695
2360
|
methods: store$1,
|
|
1696
2361
|
};
|
|
1697
2362
|
|
|
1698
|
-
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 };
|
|
1699
2364
|
//# sourceMappingURL=dv.esm.js.map
|