From 42c55c1b0936e1ca8a0b313697ba2aa59bfe78a0 Mon Sep 17 00:00:00 2001 From: patrickhlauke Date: Sun, 7 Jul 2019 23:15:55 +0100 Subject: Initial naive form validation fix --- site/content/docs/4.3/components/forms.md | 76 +++++++++++++++++++------------ 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/site/content/docs/4.3/components/forms.md b/site/content/docs/4.3/components/forms.md index 659385a73..bd2d823ec 100644 --- a/site/content/docs/4.3/components/forms.md +++ b/site/content/docs/4.3/components/forms.md @@ -745,17 +745,13 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
- -
- Looks good! -
+ +
- -
- Looks good! -
+ +
@@ -763,48 +759,38 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
@
- -
- Please choose a username. -
+ +
- -
- Please provide a valid city. -
+ +
- -
- Please select a valid state. -
+
- -
- Please provide a valid zip. -
+ +
- + -
- You must agree before submitting. -
+
@@ -825,6 +811,40 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun event.stopPropagation(); } form.classList.add('was-validated'); + + /* go through all form controls */ + var formControls = form.querySelectorAll(".form-control, form-control-range, .form-check-input"); + formControls.forEach(formControl => { + var validationContainer = formControl.dataset.validationContainer; + /* if the control has data-validation-container */ + if (validationContainer) { + var formControlDescribedby = formControl.getAttribute('aria-describedby'); + if (!formControlDescribedby) { + /* if it doesn't have aria-describedby, add it and point it to the data-validation-container */ + formControl.setAttribute('aria-describedby', validationContainer) + } else if (formControlDescribedby.indexOf(validationContainer) === -1) { + /* if it has aria-describedby, but it didn't include data-validation-container, add it */ + formControl.setAttribute('aria-describedby', formControlDescribedby + " " + validationContainer) + } + var validationContainerElement = document.getElementById(validationContainer); + /* empty the validation container */ + validationContainerElement.innerHTML = null; + /* depending on whether the control is valid or invalid, set the correct class and content for the feedback */ + if (formControl.checkValidity()) { + validationContainerElement.classList.remove('invalid-feedback'); + if (formControl.dataset.validationValid) { + validationContainerElement.innerHTML = formControl.dataset.validationValid; + validationContainerElement.classList.add('valid-feedback'); + } + } else { + validationContainerElement.classList.remove('valid-feedback'); + if (formControl.dataset.validationInvalid) { + validationContainerElement.innerHTML = formControl.dataset.validationInvalid; + validationContainerElement.classList.add('invalid-feedback'); + } + } + } + }) }, false); }); }, false); -- cgit v1.2.3