Skip to content

Commit 7d4c602

Browse files
committed
doc
1 parent b0b0745 commit 7d4c602

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

site/src/content/docs/foundation/form-validation.mdx

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,41 @@ extra_js:
1111
---
1212
import { getDocsRelativePath } from '@libs/path'
1313

14-
## Accessibility
15-
16-
Ensure that all form components have an appropriate accessible name so that their purpose can be conveyed to users of assistive technologies by using a `<label>` element linked, or—in the case of buttons—to include sufficiently descriptive text as part of the `<button>...</button>` content.
17-
18-
For situations where it’s not possible to include a visible `<label>` or appropriate text content, there are alternative ways of still providing an accessible name, such as:
19-
20-
- `<label>` elements hidden using the `.visually-hidden` class
21-
- Pointing to an existing element that can act as a label using `aria-labelledby`
22-
- Providing a `title` attribute
23-
- Explicitly setting the accessible name on an element using `aria-label`
24-
25-
If none of these are present, assistive technologies may resort to using the `placeholder` attribute as a fallback for the accessible name on `<input />` and `<textarea>` elements.{/* The examples in this section provide a few suggested, case-specific approaches. */}
26-
27-
While using visually hidden content (`.visually-hidden`, `aria-label`, and even `placeholder` content, which disappears once a form field has content) will benefit assistive technology users, a lack of visible label text may still be problematic for certain users. Some form of visible label is generally the best approach, both for accessibility and usability.
28-
2914
## How it works
3015

16+
OUDS Web provides custom feedback styles that apply custom colors, borders, focus styles, and background icons to better communicate feedback. to use them, you’ll need to add the `novalidate` boolean attribute to your `<form>`. This disables the browser default feedback tooltips, but still provides access to the form validation APIs in JavaScript.
17+
3118
Here’s how form validation works with OUDS Web:
3219

3320
- HTML form validation is applied via CSS’s two pseudo-classes, `:invalid` and `:valid`. It applies to `<input />`, `<select>`, and `<textarea>` elements.
3421
- OUDS Web scopes the `:invalid` and `:valid` styles to parent `.was-validated` class, usually applied to the `<form>`. Otherwise, any required field without a value shows up as invalid on page load. This way, you may choose when to activate them (typically after form submission is attempted).
3522
- To reset the appearance of the form (for instance, in the case of dynamic form submissions using Ajax), remove the `.was-validated` class from the `<form>` again after submission.
36-
{/* for [server-side validation](#server-side) */}
37-
- As a fallback, `.is-invalid` class may be used instead of the pseudo-classe `:invalid`. It doesn't require a `.was-validated` parent class.
38-
- All modern browsers support the [constraint validation API](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#the-constraint-validation-api), a series of JavaScript methods for validating form controls.
39-
- Feedback messages shouldn't use the browser defaults (different for each browser, and unstylable via CSS) but our custom feedback styles with additional HTML and CSS.
23+
- As a fallback, `.is-invalid` class may be used instead of the pseudo-class `:invalid` for [server-side validation](#server-side). It doesn't require a `.was-validated` parent class.
24+
- All modern browsers support the [constraint validation API](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#the-constraint-validation-api), a series of JavaScript methods for validating form controls. You may provide custom validity messages with `setCustomValidity` in JavaScript.
4025
- Feedback messages should use our custom feedback styles with additional HTML and CSS, rather than the browser defaults (which differ for each browser and can't be styled via CSS).
41-
- You may provide custom validity messages with `setCustomValidity` in JavaScript.
4226

4327
With that in mind, consider the following demos for our custom form validation styles and optional server-side classes.
4428

45-
## Custom styles
29+
## Accessibility
4630

47-
For custom OUDS Web form validation messages, you’ll need to add the `novalidate` boolean attribute to your `<form>`. This disables the browser default feedback tooltips, but still provides access to the form validation APIs in JavaScript.
31+
OUDS Web's form elements are designed to be accessible in particular by always using a `<label>` linked to a form element (beware though to use the right `id`). This allows assistive technologies to convey the purpose of each form field to users.
4832

49-
Try to submit the form below; our JavaScript will intercept the submit button, add `.was-validated` to the `<form>`, and you’ll see the `:invalid` and `:valid` styles applied to the fields. For invalid fields, it also associates the invalid feedback/error message with the relevant form field using `aria-describedby` (noting that this attribute allows more than one `id` to be referenced, in case the field already points to additional description/helper text).
33+
You must take care to specify the validation rules for each field using an appropriate attribute like `required`, `pattern`, `min`, `minLength`, etc. This allows assistive technologies to understand the validation requirements for each field and communicate them to users. The form fields will be marked as `:invalid` if the user input doesn't meet the specified validation rules.
5034

51-
Custom feedback styles apply custom colors, borders, focus styles, and background icons to better communicate feedback.
35+
To make your form validation accessible, you also have to ensure that any feedback messages are properly associated with the relevant form field using `aria-describedby` when the field becomes invalid. This is important for users of assistive technologies, as it allows them to understand what went wrong and how to fix it.
36+
37+
## Client-side
38+
39+
Try to submit the form below; our JavaScript will intercept the submit button, add `.was-validated` to the `<form>`, and you’ll see the `:invalid` and `:valid` styles applied to the fields. For invalid fields, it also associates the invalid feedback/error message with the relevant form field using `aria-describedby` (noting that this attribute allows more than one `id` to be referenced, in case the field already points to additional description/helper text).
5240

5341
<Example code={`<form class="needs-validation" novalidate>
54-
<fieldset class="control-items-list mb-2xlarge">
42+
<p>
43+
This form uses client-side validation with custom validation styles. Try to submit the form without filling it out to see them in action.
44+
</p>
45+
<p class="fw-bold">
46+
All fields are required.
47+
</p>
48+
<fieldset class="control-items-list mb-large">
5549
<legend>Title <span aria-hidden="true">*</span></legend>
5650
<div class="radio-button-item">
5751
<div class="control-item-assets-container">
@@ -82,7 +76,7 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
8276
</p>
8377
</fieldset>
8478
85-
<div class="text-input mb-2xlarge">
79+
<div class="text-input mb-large">
8680
<div class="text-input-container">
8781
<label for="username">Username <span aria-hidden="true">*</span></label>
8882
<input type="text" class="text-input-field" id="username" data-feedback-id="usernameFeedback"
@@ -93,7 +87,7 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
9387
</p>
9488
</div>
9589
96-
<div class="text-input mb-2xlarge">
90+
<div class="text-input mb-large">
9791
<div class="text-input-container">
9892
<label for="inputPassword">Password <span aria-hidden="true">*</span></label>
9993
<input type="password" id="inputPassword" class="text-input-field" data-feedback-id="passwordFeedback" placeholder=" " required>
@@ -109,7 +103,7 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
109103
</p>
110104
</div>
111105
112-
<div class="select-input mb-2xlarge">
106+
<div class="select-input mb-large">
113107
<div class="select-input-container">
114108
<label for="continent">Continent <span aria-hidden="true">*</span></label>
115109
<select class="select-input-field" id="continent" data-feedback-id="continentFeedback" required>
@@ -126,7 +120,7 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
126120
</p>
127121
</div>
128122
129-
<div class="text-area mb-2xlarge">
123+
<div class="text-area mb-large">
130124
<div class="text-area-container">
131125
<label for="comments">Comments <span aria-hidden="true">*</span></label>
132126
<textarea id="comments" class="text-area-field" data-feedback-id="commentsFeedback" required></textarea>
@@ -136,7 +130,7 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
136130
</p>
137131
</div>
138132
139-
<div class="switch-item-container mb-2xlarge">
133+
<div class="switch-item-container mb-large">
140134
<div class="switch-item control-item-reverse">
141135
<div class="control-item-assets-container">
142136
<input class="control-item-indicator" type="checkbox" role="switch" value="" id="readTermsAndConditions" data-feedback-id="readTermsAndConditionsFeedback" required />
@@ -150,7 +144,7 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
150144
</p>
151145
</div>
152146
153-
<div class="checkbox-item-container mb-2xlarge">
147+
<div class="checkbox-item-container mb-large">
154148
<div class="checkbox-item">
155149
<div class="control-item-assets-container">
156150
<input class="control-item-indicator" type="checkbox" value="" id="agreeTermsAndConditions"
@@ -177,7 +171,13 @@ We recommend using client-side validation before server-side validation. If serv
177171
For invalid fields, ensure that the invalid feedback/error message is associated with the relevant form field using `aria-describedby` (noting that this attribute allows more than one `id` to be referenced, in case the field already points to additional description/helper text).
178172

179173
<Example code={`<form novalidate>
180-
<fieldset class="control-items-list mb-2xlarge">
174+
<p>
175+
This form simulates server-side validation with custom validation styles.
176+
</p>
177+
<p class="fw-bold">
178+
All fields are required.
179+
</p>
180+
<fieldset class="control-items-list mb-large">
181181
<legend>Title <span aria-hidden="true">*</span></legend>
182182
<div class="radio-button-item">
183183
<div class="control-item-assets-container">
@@ -208,7 +208,7 @@ For invalid fields, ensure that the invalid feedback/error message is associated
208208
</p>
209209
</fieldset>
210210
211-
<div class="text-input mb-2xlarge">
211+
<div class="text-input mb-large">
212212
<div class="text-input-container">
213213
<label for="username2">Username <span aria-hidden="true">*</span></label>
214214
<input type="text" class="text-input-field is-invalid" id="username2" aria-describedby="usernameFeedback"
@@ -219,7 +219,7 @@ For invalid fields, ensure that the invalid feedback/error message is associated
219219
</p>
220220
</div>
221221
222-
<div class="text-input mb-2xlarge">
222+
<div class="text-input mb-large">
223223
<div class="text-input-container">
224224
<label for="inputPassword2">Password <span aria-hidden="true">*</span></label>
225225
<input type="password" id="inputPassword2" class="text-input-field is-invalid" aria-describedby="passwordFeedback" placeholder=" " required>
@@ -235,7 +235,7 @@ For invalid fields, ensure that the invalid feedback/error message is associated
235235
</p>
236236
</div>
237237
238-
<div class="select-input mb-2xlarge">
238+
<div class="select-input mb-large">
239239
<div class="select-input-container">
240240
<label for="continent2">Continent <span aria-hidden="true">*</span></label>
241241
<select class="select-input-field is-invalid" id="continent2" aria-describedby="continentFeedback" required>
@@ -252,7 +252,7 @@ For invalid fields, ensure that the invalid feedback/error message is associated
252252
</p>
253253
</div>
254254
255-
<div class="text-area mb-2xlarge">
255+
<div class="text-area mb-large">
256256
<div class="text-area-container">
257257
<label for="comments2">Comments <span aria-hidden="true">*</span></label>
258258
<textarea id="comments2" class="text-area-field is-invalid" aria-describedby="commentsFeedback" required></textarea>
@@ -262,7 +262,7 @@ For invalid fields, ensure that the invalid feedback/error message is associated
262262
</p>
263263
</div>
264264
265-
<div class="switch-item-container mb-2xlarge">
265+
<div class="switch-item-container mb-large">
266266
<div class="switch-item control-item-reverse">
267267
<div class="control-item-assets-container">
268268
<input class="control-item-indicator is-invalid" type="checkbox" role="switch" value="" id="readTermsAndConditions2" aria-describedby="readTermsAndConditionsFeedback" required />
@@ -276,7 +276,7 @@ For invalid fields, ensure that the invalid feedback/error message is associated
276276
</p>
277277
</div>
278278
279-
<div class="checkbox-item-container mb-2xlarge">
279+
<div class="checkbox-item-container mb-large">
280280
<div class="checkbox-item">
281281
<div class="control-item-assets-container">
282282
<input class="control-item-indicator is-invalid" type="checkbox" value="" id="agreeTermsAndConditions2"

0 commit comments

Comments
 (0)