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
Copy file name to clipboardExpand all lines: packages/react-aria-components/docs/Form.mdx
+57Lines changed: 57 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -160,6 +160,63 @@ To provide validation errors, the `validationErrors` prop should be set to an ob
160
160
161
161
See the [Forms](forms.html) guide to learn more about form validation in React Aria, including client-side validation, and integration with other frameworks and libraries.
162
162
163
+
### Validation behavior
164
+
165
+
By default, native HTML form validation is used to display errors and block form submission.
166
+
167
+
```tsx example
168
+
<Form>
169
+
<TextFieldname="email"type="email"isRequired>
170
+
<Label>Email</Label>
171
+
<Input />
172
+
<FieldError />
173
+
</TextField>
174
+
<divstyle={{display: 'flex', gap: 8}}>
175
+
<Buttontype="submit">Submit</Button>
176
+
<Buttontype="reset">Reset</Button>
177
+
</div>
178
+
</Form>
179
+
```
180
+
181
+
To instead use ARIA attributes for form validation, set the validationBehavior prop to "aria". This will not block form submission, and will ensure validation errors are displayed to the user in realtime as the value is edited.
182
+
183
+
This can be set at the form level to apply to all fields, or at the field level to override the form's behavior for a specific field.
The `validationBehavior` for a Form can be accessed from within any component inside a given From by using the `FormContext`.
213
+
214
+
```tsx
215
+
import {FormContext} from'react-aria-components';
216
+
217
+
let {validationBehavior} =useContext(FormContext);
218
+
```
219
+
163
220
### Focus management
164
221
165
222
By default, after a user submits a form with validation errors, the first invalid field will be focused. You can prevent this by calling `preventDefault` during the `onInvalid` event, and move focus yourself. This example shows how to move focus to an [alert](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/alert_role) element at the top of a form.
0 commit comments