Files
overleaf-cep/services/web/frontend/js/features/form-helpers/input-validator.js
T
Rebeka Dekany c9a1ecd060 Remove Bootstrap version 3 (#27420)
* Remove bootstrap-3 entrypoint

* Rename bootstrap-5 entrypoint to bootstrap

* Restore entrypoints

* Remove `bootstrap-5` and `bootstrap-3` entrypoints and a single `bootstrap.ts` file is now the default entrypoint

* Update `bootstrap-5` component imports to `bootstrap`

* Update `bootstrap-5` CSS imports to `bootstrap`

* Remove the `isBootstrap5` utility

* Remove `bootstrapVersion`

* Remove `ol-bootstrapVersion`

* Remove getCssThemeModifier

* Update path and rename

* Source format

* Remove Bootstrap v3 and Bootstrap v5 alias npm packages

* Remove bootstrap argument

* Remove unused files

* Update with the latest split tests

* Remove remaining bootstrap5PageStatus

* Update path

GitOrigin-RevId: 7acda2f80114d2de8699e1f06729a2a29218e284
2025-08-08 08:06:00 +00:00

74 lines
2.3 KiB
JavaScript

import { materialIcon } from '@/features/utils/material-icon'
export default function inputValidator(inputEl) {
const messageEl = document.createElement('div')
messageEl.className =
inputEl.getAttribute('data-ol-validation-message-classes') ||
'small text-danger mt-2 form-text'
messageEl.hidden = true
const messageInnerEl = messageEl.appendChild(document.createElement('span'))
messageInnerEl.className = 'form-text-inner'
const messageTextNode = document.createTextNode('')
const iconEl = materialIcon('error')
messageInnerEl.append(iconEl)
messageInnerEl.append(messageTextNode)
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()
}
function displayValidationMessages() {
// Display all the error messages and highlight fields with red border.
canDisplayErrorMessages = true
updateValidationMessageContent()
}
function updateValidationMessageContent() {
if (!canDisplayErrorMessages) return
if (!inputEl.hasAttribute('data-ol-dirty')) return
if (inputEl.validity.valid) {
messageEl.hidden = true
// Require another blur before displaying errors again.
canDisplayErrorMessages = false
} else {
messageTextNode.data = inputEl.validationMessage
messageEl.hidden = false
}
}
}