-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release-10.1] Fix form validation (#4227)
The old validation mechanism was not compatible with Bootstrap 5 and didn't work properly with Bootstrap 3 either. The new uses the JS validation API. It is theme-independent and less intrusive with the caveat that the validation error messages are displayed in a browser-specific style.
- Loading branch information
1 parent
76f1b27
commit 9f0985e
Showing
31 changed files
with
210 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* global VuFind, isPhoneNumberValid */ | ||
|
||
VuFind.register('validation', function Validation() { | ||
/** | ||
* Check field match validity | ||
* @param {Event} ev Event | ||
* @returns {boolean} Validity | ||
*/ | ||
function checkMatchValidity(ev) { | ||
const field = ev.target; | ||
const matchSelector = field.dataset.match; | ||
if (!matchSelector) { | ||
return true; | ||
} | ||
const matchEl = field.form.querySelector(matchSelector); | ||
if (matchEl.value !== field.value) { | ||
field.setCustomValidity(field.dataset.matchError || ''); | ||
return false; | ||
} | ||
field.setCustomValidity(''); | ||
return true; | ||
} | ||
|
||
/** | ||
* Check field phone number validity | ||
* @param {Event} ev Event | ||
* @returns {boolean} Validity | ||
*/ | ||
function checkPhoneNumberValidity(ev) { | ||
const field = ev.target; | ||
if (field.id && field.type === 'tel' && field.dataset.validatorRegion) { | ||
const valid = isPhoneNumberValid(field.value, field.dataset.validatorRegion); | ||
if (true !== valid) { | ||
field.setCustomValidity(typeof valid === 'string' ? valid : VuFind.translate('libphonenumber_invalid')); | ||
return false; | ||
} | ||
} | ||
field.setCustomValidity(''); | ||
return true; | ||
} | ||
|
||
/** | ||
* Check field validity | ||
* @param {Event} ev Event | ||
*/ | ||
function checkValidity(ev) { | ||
const checks = [checkMatchValidity, checkPhoneNumberValidity]; | ||
for (const check of checks) { | ||
if (!check(ev)) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Init custom form validation | ||
*/ | ||
function init() { | ||
document.addEventListener('input', checkValidity); | ||
} | ||
|
||
return { | ||
init: init | ||
}; | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.