Admin forms, drafts, and validation
Connect a record, an editable draft, and a write request with AppForm, then verify async validation and grouped editing.
Use AppForm to turn an API record into a draft and map the validated draft back to a write request. Grouped forms merge drafts across groups. You can show field and form errors at their respective levels.
Turn a record into an editable draft
Consider an editor with Details and Media tabs. Map the API record to defaultValues, then declare its controls and paths with FormFieldSchema, such as media.caption. When switching records, you can use the record ID as the React key on AppForm to start a new form instance.
Use visibleWhen to control when a field appears. In the submit mapper, explicitly decide which fields to keep or remove from the API input. Service validates the received payload, including permissions, ownership, and cross-field constraints.
Handle validation and save errors at their source
| Failure | Where to handle it |
|---|---|
| Missing title or invalid field format | Declare rules with a Zod schema. A supplied resolver takes precedence |
| Two fields are valid only in combination | Return issues with path and message from onValidate |
| The write endpoint rejects a field | Map the applicable API error to a field path with onSubmitError |
| Revision conflict or error outside a specific control | Surface the issue at root so the editor can see how to proceed |
onSubmit receives the validated draft snapshot. Map that snapshot to the API input and await the write. After success, refresh affected lists, details, and resource pickers. If values change during asynchronous validation, the current submission stops and the user is prompted to submit again.
Verify grouped forms
With tab-group, step-group, or tag-group, assign fields with group. Validation focuses on visible fields in the active group, and submission merges declared drafts across groups. Exercise the sequence below:
- Edit the title, switch to Media and choose another image, then switch back and check the title.
- Force a save error and check that the draft remains editable and the error appears where the editor can act on it.
- Save, reopen the record, and check both groups and the Admin list.
- Make another edit and reset. Check that the form restores the committed values.
The implementation starts in apps/admin/src/common/form/form.tsx. To connect a form to a feature, see pages and data views.