You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
chore: Add Docs for Rendering Error Messages to Stateful Objects
This will especially be helpful for React developers who
don't want to think about using `useMemo`/`React.memo`,
though hopefully `React Forget` will eradicate those
concerns sometime soon.
-[Keeping Track of Visited/Dirty Fields](#keeping-track-of-visiteddirty-fields)
7
7
-[Getting the Most out of the `defaultErrors` option](#getting-the-most-out-of-the-defaulterrors-option)
8
+
-[Managing Form Errors with State](#managing-form-errors-with-state)
8
9
-[Keeping Track of Form Data](#keeping-track-of-form-data)
9
10
-[Recommendations for Conditionally Rendered Fields](#recommendations-for-conditionally-rendered-fields)
10
11
-[Recommendations for Styling Form Fields and Their Error Messages](#recommendations-for-styling-form-fields-and-their-error-messages)
@@ -13,9 +14,8 @@ Here you'll find helpful tips on how to use the `FormValidityObserver` effective
13
14
<!--
14
15
TODO: Some `Guides` that could be helpful:
15
16
16
-
1) Using `renderByDefault` to render errors to stateful objects (instead of rendering to the DOM).
17
-
2) Reconciling error messages sent by the server on the client side.
18
-
3) MAYBE something on how to work with accessible error messages? (Should we also mention `aria-errormessage` vs. `aria-describedby` too? As well as the lack of support for `aria-errormessage`? Or does that belong somewhere else in the docs?)
17
+
1) Reconciling error messages sent by the server on the client side.
18
+
2) MAYBE something on how to work with accessible error messages? (Should we also mention `aria-errormessage` vs. `aria-describedby` too? As well as the lack of support for `aria-errormessage`? Or does that belong somewhere else in the docs?)
19
19
-->
20
20
21
21
## Enabling Accessible Error Messages during Form Submissions
@@ -276,6 +276,87 @@ const observer = new FormValidityObserver("focusout", {
276
276
});
277
277
```
278
278
279
+
## Managing Form Errors with State
280
+
281
+
Typically, the `FormValidityObserver` renders error messages directly to the DOM when an [accessible error container](https://www.w3.org/WAI/WCAG21/Techniques/aria/ARIA21#example-2-identifying-errors-in-data-format) is present. But if you prefer to render your error messages in a different way (or to a different location), then you can leverage the observer's [`renderer`](./README.md#form-validity-observer-options-renderer) option to do so.
282
+
283
+
For example, you might want to rely on React State (or another JS framework's state) to display error messages to your users. In that case, you can pass a `renderer` function to the `FormValidityObserver` that updates your local error state instead of updating the DOM. From there, you can let your framework do the work of updating the UI.
284
+
285
+
> Note: We generally recommend keeping your forms stateless when possible, but sometimes your use case might require you to use state.
With this approach, our error messages are "rendered" to our stateful `errors` object instead of being rendered to the DOM directly. Then, we let the JavaScript framework take responsibility for displaying any error messages that are present.
355
+
356
+
Notice that we also supplied the [`renderByDefault:true`](./README.md#form-validity-observer-options-render-by-default) option to the `FormValidityObserver`. This option is important because it causes all error messages that are pure strings to be sent through our `renderer` function by default -- including the browser's default error messages. In other words, this option guarantees that all error messages which are generated for our form fields will be properly assigned to our stateful error object.
357
+
358
+
You can find more detailed examples of using stateful error objects on [StackBlitz](https://stackblitz.com/@ITenthusiasm/collections/form-observer-examples).
359
+
279
360
## Keeping Track of Form Data
280
361
281
362
Many form libraries offer stateful solutions for managing the data in your forms as JSON. But there are a few disadvantages to this approach:
0 commit comments