Skip to content

Commit

Permalink
Read validate-forms.js from assets.
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Jun 3, 2020
1 parent 6824d67 commit 358d637
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
26 changes: 6 additions & 20 deletions site/content/docs/5.0/forms/validation.md
Expand Up @@ -4,6 +4,9 @@ title: Validation
description: Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript.
group: forms
toc: true
extra_js:
- src: "/docs/5.0/assets/js/validate-forms.js"
async: true
---

{{< callout warning >}}
Expand Down Expand Up @@ -99,26 +102,9 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
{{< /example >}}

{{< example lang="js" show_preview="false" >}}
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function () {
'use strict';

// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation');

// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!form.checkValidity()) {
event.preventDefault();
event.stopPropagation();
}

form.classList.add('was-validated');
}, false);
});
})();
{{< js.inline >}}
{{- readFile (printf "site/static/docs/%s/assets/js/validate-forms.js" .Site.Params.docs_version) -}}
{{< /js.inline >}}
{{< /example >}}

## Browser defaults
Expand Down
4 changes: 4 additions & 0 deletions site/layouts/_default/docs.html
Expand Up @@ -47,5 +47,9 @@ <h1 class="bd-title" id="content">{{ .Title | markdownify }}</h1>

{{ partial "footer" . }}
{{ partial "scripts" . }}

{{ range .Page.Params.extra_js -}}
<script{{ with .async }} async{{ end }} src="{{ .src }}"></script>
{{- end -}}
</body>
</html>
Expand Up @@ -5,10 +5,6 @@
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation')

if (!forms) {
return
}

// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function (form) {
Expand Down

0 comments on commit 358d637

Please sign in to comment.