Build a form that saves through the API
Any form that collects fields and submits them to the API uses the shared form scaffold: create and edit forms, renames, and confirm or delete dialogs that can fail with a server message. The scaffold provides validation, field errors, the form banner, and focus handling. Its exports and behavior are in Dashboard UI and forms.
Controls that only change local state, such as the scenario sliders of a document written in code, can use React state. Parameter writes follow Write parameter values from a dashboard.
Build the form
Section titled “Build the form”- Create the form with
useAppForm. Do not use bareuseFormfrom@tanstack/react-form. - Set real
defaultValues. For a dialog, mount the form when the dialog opens and unmount it when the dialog closes, so it starts from the current values each time. - Set
validators: mutationFormValidators({ schema, submit }):schemais the shared TypeBox form schema. Omit it only in a dialog that only confirms.submitcalls the mutation and returnsnull.- When the form field names differ from the API input, pass
mapError: (error, value) => mapTrpcToFormErrors(error, value, { fieldMap }). KeepmapErrorpure, because it runs again whenever the message is read. Put side effects of a failure inonError.
- Set
onSubmitInvalid: focusFirstInvalidField. - Put success side effects, such as a success toast, navigation, or closing the dialog, in
onSubmitonly. - Render
form.AppForm, thenform.FormRoot, then the fields andform.SubmitButton. - When a submitted value must be converted to the API input, use the shared
…FormToInputfunction and show its problem withformInputProblemErrors(result, message).
Confirm and delete dialogs use the same scaffold.
Show errors where operators look for them
Section titled “Show errors where operators look for them”- Leave field errors to the scaffold: validation issues and API errors that name a field appear on that field, and other API failures appear in the form banner.
- Do not show a field-bearing submit’s error in a toast. Use error toasts only for actions without fields, such as an enable switch or a copy to the clipboard.
- Show API errors outside a form with
apiErrorMessage, which reads the host catalog’serror_Xmessage for codeX. Translate the dashboard’s own error texts in its own message map.
Save the dashboard’s own configuration
Section titled “Save the dashboard’s own configuration”- Use
useDashboardConfigurationMutationwith the dashboard’s TypeBox configuration schema and a transform of the stored configuration. It fails instead of overwriting a configuration that changed since it was read. - For labels stored in the configuration, use
DashboardConfigurationRenameDialog. - For a parameter UUID field, use
ParameterPickerField.
Check the form
Section titled “Check the form”- A field-level API error shows on the field, not only in a toast.
- Other API failures show in the form banner (
FormErrorBanner). - An invalid submit focuses the first invalid field.
- A dialog form opens again with correct defaults.
- API errors show translated text, never the English
message.