diff --git a/docs/_includes/sidebar.njk b/docs/_includes/sidebar.njk index 6df6aa3cb..496824ab2 100644 --- a/docs/_includes/sidebar.njk +++ b/docs/_includes/sidebar.njk @@ -5,6 +5,7 @@
  • Usage
  • Themes
  • Customizing
  • +
  • Form Controls
  • Localization
  • diff --git a/docs/docs/form-controls.md b/docs/docs/form-controls.md new file mode 100644 index 000000000..fd3aafcc9 --- /dev/null +++ b/docs/docs/form-controls.md @@ -0,0 +1,179 @@ +--- +title: Form Controls +description: Using Web Awesome form controls. +layout: page-outline +--- + +Web Awesome form controls are form-associated custom elements, meaning they will submit with forms just like native `
    ` controls. They also support constraint validation, which is the platform's version of client-side form validation. + +## Constraint Validation + +Client-side validation can be enabled through the browser's [Constraint Validation API](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation) for Web Awesome form controls. You can activate it using attributes such as `required`, `pattern`, `minlength`, `maxlength`, etc. Web Awesome implements many of the same attributes as native form controls, but check the documentation for a list of supported properties for each component. + +If you don't want to use client-side validation, you can suppress this behavior by adding `novalidate` to the surrounding `` element. + +:::info +If this syntax looks unfamiliar, don't worry! Most of what you're learning on this page is platform knowledge that applies to regular form controls, too. +::: + +:::warning +Client-side validation can be used to improve the UX of forms, but it is not a replacement for server-side validation. **You should always validate and sanitize user input on the server!** +::: + +### Required Fields + +To make a field required, use the `required` attribute. Required fields will automatically receive an asterisk after their labels. The form will not be submitted if a required field is incomplete. + +```html {.example} + + +
    + + Birds + Cats + Dogs + Other + +
    + +
    + Check me before submitting +

    + Submit +
    + + +``` + +### Input Patterns + +To restrict a value to a specific [pattern](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern), use the `pattern` attribute. This example only allows the letters A-Z, so the form will not submit if a number or symbol is entered. This only works with `` elements. + +```html {.example} +
    + +
    + Submit + Reset +
    + + +``` + +### Input Types + +Some input types will automatically trigger constraints, such as `email` and `url`. + +```html {.example} +
    + +
    + +
    + Submit + Reset +
    + + +``` + +### Custom Error Messages + +To create a custom validation error, pass a non-empty string to the `setCustomValidity()` method. This will override any existing validation constraints. The form will not be submitted when a custom validity is set and the browser will show a validation error when the containing form is submitted. To make the input valid again, call `setCustomValidity()` again with an empty string. + +```html {.example} +
    + +
    + Submit + Reset +
    + + +``` + +:::info +Custom validation can be applied to any form control that supports the `setCustomValidity()` method. It is not limited to inputs and textareas. +::: + +## Custom Validation Styles + +Due to the many ways form controls are used, Web Awesome doesn't provide out of the box validation styles for form controls as part of its default theme. Instead, the following attributes will be applied to reflect a control's validity as users interact with it. You can use them to create custom styles for any of the validation states you're interested in. + +- `data-wa-required` - the form control is required +- `data-wa-optional` - the form control is optional +- `data-wa-invalid` - the form control is invalid +- `data-wa-valid` - the form control is valid +- `data-wa-user-invalid` - the form control is invalid and the user has interacted with it +- `data-wa-user-valid` - the form control is valid and the user has interacted with it + +These attributes map to the browser's built-in pseudo classes for validation: [`:required`](https://developer.mozilla.org/en-US/docs/Web/CSS/:required), [`:optional`](https://developer.mozilla.org/en-US/docs/Web/CSS/:optional), [`:invalid`](https://developer.mozilla.org/en-US/docs/Web/CSS/:invalid), [`:valid`](https://developer.mozilla.org/en-US/docs/Web/CSS/:valid), [`:user-invalid`](https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid), and [`:user-valid`](https://developer.mozilla.org/en-US/docs/Web/CSS/:user-valid). + +:::info +In the future, data attribute selectors will be replaced with custom states such as `:state(valid)` and `:state(invalid)`. Web Awesome is using data attributes as a workaround until browsers fully support [custom states](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/states). +:::