-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Why**: To consolidate the various client side methods we are using.
- Loading branch information
Showing
9 changed files
with
103 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,83 @@ | ||
import 'classlist.js'; | ||
import gf from 'gentleform'; | ||
import h5f from 'h5f'; | ||
|
||
|
||
function validateForm() { | ||
const form = document.querySelector('form[novalidate]'); | ||
const validate = { | ||
msgs: { | ||
missing: 'Please fill in all required fields.', | ||
mismatch: 'Please match the requested format.', | ||
}, | ||
|
||
if (form) { | ||
gf(form, function onSubmit(e) { | ||
if (!this.isValid()) e.preventDefault(); | ||
init() { | ||
this.form = document.querySelector('form'); | ||
if (!this.form) return; | ||
this.btn = this.form.querySelector('[type=submit]'); | ||
|
||
h5f.setup(this.form, { | ||
validClass: 'valid', | ||
invalidClass: 'invalid', | ||
requiredClass: 'required', | ||
placeholderClass: 'placeholder', | ||
}); | ||
} | ||
} | ||
|
||
this.addEvents(); | ||
}, | ||
|
||
addEvents() { | ||
this.form.addEventListener('invalid', e => e.preventDefault(), true); | ||
this.form.addEventListener('change', e => this.validateField(e.target)); | ||
this.form.addEventListener('submit', e => this.submitForm(e)); | ||
this.btn.addEventListener('click', () => this.validateForm()); | ||
}, | ||
|
||
submitForm(e) { | ||
if (!this.form.checkValidity()) e.preventDefault(); | ||
}, | ||
|
||
validateForm() { | ||
const fields = this.form.querySelectorAll('.field'); | ||
for (let i = 0; i < fields.length; i++) { | ||
this.validateField(fields[i]); | ||
} | ||
|
||
// add focus to first invalid input | ||
const invalidField = this.form.querySelector(':invalid'); | ||
if (invalidField) invalidField.focus(); | ||
}, | ||
|
||
validateField(f) { | ||
f.classList.add('interacted'); | ||
|
||
const parent = f.parentNode; | ||
const errorMsg = parent.querySelector('.error-message'); | ||
|
||
if (errorMsg !== null) parent.removeChild(errorMsg); | ||
|
||
if (!f.validity.valid) this.addInvalidMarkup(f); | ||
else this.removeInvalidMarkup(f); | ||
}, | ||
|
||
addInvalidMarkup(f) { | ||
f.setAttribute('aria-invalid', 'true'); | ||
f.setAttribute('aria-describedby', `alert_${f.id}`); | ||
|
||
if (f.validity.valueMissing) f.setCustomValidity(this.msgs.missing); | ||
else if (f.validity.typeMismatch) f.setCustomValidity(this.msgs.mismatch); | ||
else f.setCustomValidity(''); | ||
|
||
f.insertAdjacentHTML( | ||
'afterend', | ||
`<div role='alert' class='error-message red h5' id='alert_${f.id}'> | ||
${f.validationMessage} | ||
</div>` | ||
); | ||
}, | ||
|
||
removeInvalidMarkup(f) { | ||
f.removeAttribute('aria-invalid'); | ||
f.removeAttribute('aria-describedby'); | ||
}, | ||
}; | ||
|
||
|
||
document.addEventListener('DOMContentLoaded', validateForm); | ||
document.addEventListener('DOMContentLoaded', () => validate.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import 'app/utils'; | ||
import 'app/pw-toggle'; | ||
import 'app/form-validation'; | ||
import 'app/form-field-format'; |
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