Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix forms validation #30706

Merged
merged 3 commits into from Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 8 additions & 22 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 @@ -96,29 +99,12 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</form>
{{< /example >}}

<script>
// 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);
});
})();
</script>
{{< example lang="js" show_preview="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>
3 changes: 2 additions & 1 deletion site/layouts/shortcodes/example.html
Expand Up @@ -8,6 +8,7 @@
show_markup: if the markup should be output in the HTML - default: `true`
*/ -}}

{{- $lang := .Get "lang" | default "html" -}}
{{- $show_preview := .Get "show_preview" | default true -}}
{{- $show_markup := .Get "show_markup" | default true -}}
{{- $input := .Inner -}}
Expand All @@ -21,5 +22,5 @@
{{- if eq $show_markup true -}}
{{- $content := replaceRE `<svg class="bd\-placeholder\-img(?:\-lg)?(?: *?bd\-placeholder\-img\-lg)? ?(.*?)".*?<\/svg>\n` `<img src="..." class="$1" alt="...">` $input -}}
{{- $content = replaceRE `(class=" *?")` "" $content -}}
{{- highlight (trim $content "\n") "html" "" -}}
{{- highlight (trim $content "\n") $lang "" -}}
{{- end -}}
20 changes: 20 additions & 0 deletions 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)
})
})()