Skip to main content

useSubmitForm

Wraps a form submit handler in the notification and error handling the generated forms use.

Use it for a form you built by hand — a settings screen, a custom dialog — so its success and error messages look like the rest of the admin and its field errors land on the right inputs.

Signature​

import {useSubmitForm} from 'habarico';

const {handleSubmit, isLoading} = useSubmitForm(
onSubmit,
onSuccess,
onError,
successMessage,
errorMessage,
tooManyRequestsMessage
);

Parameters​

Positional, not an options object.

#NameTypeDescription
1onSubmit (required)SubmitHandler<T>Does the work; throw to signal failure
2onSuccess() => voidCalled after a successful submit, once the notification is shown
3onError(error: Error) => voidCalled on every failure, including validation ones
4successMessagestring | nullMessage on success; null shows none. Defaults to ra.message.successfully_done
5errorMessagestringFallback message when the error carries no text of its own. Defaults to ra.message.invalid_form
6tooManyRequestsMessagestringMessage shown specifically for a 429

Returns​

NameTypeDescription
handleSubmit(data: FieldValues) => Promise<…>Pass it to react-hook-form's handleSubmit
isLoadingbooleanTrue while the submit is in flight — bind it to the save button

When the backend answers with field errors (body.errors), handleSubmit returns them so react-hook-form can attach them to the matching inputs. A submit that succeeds returns nothing.

Behaviour​

The failure is classified before it is reported:

FailureWhat the user sees
429 and tooManyRequestsMessage is setThat message
HttpError with a non-empty body.errorsNothing global — the errors go to the fields
HttpError with an empty body.errorsThe message extracted from the error, else errorMessage
Anything elseThe same extracted-or-fallback message

isLoading is cleared in a finally, so it is safe even when onSubmit throws.

See also​

  • FormShell — the save toolbar to pair it with
  • SchemaEdit — the generated form doing the same thing
  • useCallAction — running a backend action instead of saving a form