added dependant fields; fixed disabled fields getting a required indicator on page load

This commit is contained in:
2025-04-12 00:54:08 -04:00
parent f842fbf9b9
commit 5f9ab78353
2 changed files with 29 additions and 2 deletions

View File

@ -1,4 +1,5 @@
class ValidationField {
dependant = null
el = null
formGroupEl = null
isValid = null
@ -95,10 +96,24 @@ class ValidationField {
// add a native pattern attr if pattern property is present
if (this.pattern) this.el.setAttribute('pattern', this.pattern)
// dependant fields
if (this.dependant) {
const parent = this.validationInstance.form.querySelector(`[name=${this.dependant}]`)
const depCheck = () => {
parent.value
? this.el.removeAttribute('disabled')
: this.el.setAttribute('disabled', 'disabled')
}
depCheck()
parent.addEventListener('input', depCheck)
}
// add required indicator
if (this.validationInstance.reqIndicators) {
const hasIndicator = this.formGroupEl?.querySelector(`.${this.validationInstance.reqIndicatorClass}`)
if (!hasIndicator && !this.optional) {
if (!hasIndicator && !this.optional && !this.el.disabled) {
this.labelEl?.insertAdjacentHTML(
'beforeend',
`<span class="${this.validationInstance.reqIndicatorClass}">${this.validationInstance.reqIndicator}</span>`