From 358d637d1c664950fea501f928cc9f0247f2b8f0 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sat, 2 May 2020 18:49:12 +0300 Subject: Read validate-forms.js from assets. --- site/assets/js/validate-forms.js | 24 ---------------------- site/content/docs/5.0/forms/validation.md | 26 ++++++------------------ site/layouts/_default/docs.html | 4 ++++ site/static/docs/5.0/assets/js/validate-forms.js | 20 ++++++++++++++++++ 4 files changed, 30 insertions(+), 44 deletions(-) delete mode 100644 site/assets/js/validate-forms.js create mode 100644 site/static/docs/5.0/assets/js/validate-forms.js diff --git a/site/assets/js/validate-forms.js b/site/assets/js/validate-forms.js deleted file mode 100644 index b79f63f2d..000000000 --- a/site/assets/js/validate-forms.js +++ /dev/null @@ -1,24 +0,0 @@ -// 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') - - if (!forms) { - return - } - - // 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) - }) -})() diff --git a/site/content/docs/5.0/forms/validation.md b/site/content/docs/5.0/forms/validation.md index c50516362..d70507037 100644 --- a/site/content/docs/5.0/forms/validation.md +++ b/site/content/docs/5.0/forms/validation.md @@ -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 >}} @@ -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 diff --git a/site/layouts/_default/docs.html b/site/layouts/_default/docs.html index 01890869a..2e5bac7fc 100644 --- a/site/layouts/_default/docs.html +++ b/site/layouts/_default/docs.html @@ -47,5 +47,9 @@ {{ partial "footer" . }} {{ partial "scripts" . }} + + {{ range .Page.Params.extra_js -}} + + {{- end -}} diff --git a/site/static/docs/5.0/assets/js/validate-forms.js b/site/static/docs/5.0/assets/js/validate-forms.js new file mode 100644 index 000000000..f8fd583de --- /dev/null +++ b/site/static/docs/5.0/assets/js/validate-forms.js @@ -0,0 +1,20 @@ +// 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) + }) +})() -- cgit v1.2.3