2021-09-15 04:42:56 -04:00
|
|
|
export default function inputValidator(inputEl) {
|
2022-12-06 05:22:20 -05:00
|
|
|
const messageEl = document.createElement('div')
|
2021-09-15 04:42:56 -04:00
|
|
|
messageEl.className =
|
|
|
|
inputEl.getAttribute('data-ol-validation-message-classes') ||
|
2022-12-06 05:22:20 -05:00
|
|
|
'small text-danger mt-2'
|
2021-09-15 04:42:56 -04:00
|
|
|
messageEl.hidden = true
|
|
|
|
inputEl.insertAdjacentElement('afterend', messageEl)
|
|
|
|
|
|
|
|
// Hide messages until the user leaves the input field or submits the form.
|
|
|
|
let canDisplayErrorMessages = false
|
|
|
|
|
|
|
|
// Handle all kinds of inputs.
|
|
|
|
inputEl.addEventListener('input', handleUpdate)
|
|
|
|
inputEl.addEventListener('change', handleUpdate)
|
|
|
|
|
|
|
|
// The user has left the input field.
|
|
|
|
inputEl.addEventListener('blur', displayValidationMessages)
|
|
|
|
|
|
|
|
// The user has submitted the form and the current field has errors.
|
|
|
|
inputEl.addEventListener('invalid', e => {
|
|
|
|
// Block the display of browser error messages.
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
// Force the display of messages.
|
|
|
|
inputEl.setAttribute('data-ol-dirty', '')
|
|
|
|
|
|
|
|
displayValidationMessages()
|
|
|
|
})
|
|
|
|
|
|
|
|
function handleUpdate() {
|
|
|
|
// Mark an input as "dirty": the user has typed something in at some point
|
|
|
|
inputEl.setAttribute('data-ol-dirty', '')
|
|
|
|
|
|
|
|
// Provide live updates to content sensitive error message like this:
|
|
|
|
// Please include an '@' in the email address. 'foo' is missing an '@'.
|
|
|
|
// We should not leave a stale message as the user types.
|
|
|
|
updateValidationMessageContent()
|
|
|
|
}
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-15 04:42:56 -04:00
|
|
|
function displayValidationMessages() {
|
|
|
|
// Display all the error messages and highlight fields with red border.
|
|
|
|
canDisplayErrorMessages = true
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-15 04:42:56 -04:00
|
|
|
updateValidationMessageContent()
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
|
2021-09-15 04:42:56 -04:00
|
|
|
function updateValidationMessageContent() {
|
|
|
|
if (!canDisplayErrorMessages) return
|
|
|
|
if (!inputEl.hasAttribute('data-ol-dirty')) return
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-15 04:42:56 -04:00
|
|
|
if (inputEl.validity.valid) {
|
|
|
|
messageEl.hidden = true
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-15 04:42:56 -04:00
|
|
|
// Require another blur before displaying errors again.
|
|
|
|
canDisplayErrorMessages = false
|
2021-09-08 05:26:18 -04:00
|
|
|
} else {
|
2021-09-15 04:42:56 -04:00
|
|
|
messageEl.textContent = inputEl.validationMessage
|
|
|
|
messageEl.hidden = false
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|