useFormSubmit
Handles everything that happens when the user clicks “Submit”: plugin transforms, onBeforeSubmit hooks, modular submit actions, onAfterSubmit, error handling, redirect, and success/error messages.
You probably don’t need this directly.
useFormStatecalls it internally. Use it only when building a custom submit flow outside the standard<Form />component.
Signature
import { useFormSubmit } from '@saastro/forms';
const {
loading,
submitted,
error,
submitActionsResult,
handleSubmit,
resetError,
getSuccessMessage,
getErrorMessage,
} = useFormSubmit({ methods, config });
Parameters
| Parameter | Type | Description |
|---|---|---|
methods | UseFormReturn | React Hook Form instance |
config | FormConfig | Full form configuration (includes submit actions, plugins, messages) |
Return Value
| Property | Type | Description |
|---|---|---|
loading | boolean | true while submission is in progress |
submitted | boolean | true after successful submission |
error | Error | null | Last submission error |
submitActionsResult | SubmitActionsResult | null | Results from modular submit actions |
handleSubmit | (data) => Promise<void> | The submit handler |
resetError | () => void | Clear the error state |
getSuccessMessage | () => string | Resolve success message (static or dynamic) |
getErrorMessage | () => string | Resolve error message (static or dynamic) |
Submit Flow
When handleSubmit(data) is called:
- Sets
loading = true, clears previous error - Plugin transform:
pluginManager.transformValues(data)producesprocessedValues - Plugin hook:
onBeforeSubmit(processedValues)— if it throws, submission is cancelled - Submit actions: Resolves
onSubmit-triggered actions and runs them viaexecuteSubmitActions(). Throws if any action fails - Legacy fallback: If no submit actions exist, falls back to
config.submit.onSubmit()ordefaultSubmit() - Plugin hook:
onAfterSubmit(processedValues, response) - Sets
submitted = true, firesconfig.onSuccess(processedValues) - Redirect: If
config.redirectis set (string or function), navigates viawindow.location.href - Error path: Normalizes error, fires
onErrorplugin hook andconfig.onError, shows toast - Sets
loading = false
Messages
getSuccessMessage() and getErrorMessage() resolve the configured messages:
// Static
.successMessage('Thanks for submitting!')
// Dynamic — receives form values
.successMessage((values) => `Thanks ${values.name}!`)
// Dynamic error — receives error + values
.errorMessage((error, values) => `Failed for ${values.email}: ${error.message}`)
Defaults: 'Gracias!' for success, 'Ha ocurrido un error al enviar el formulario.' for error.
Related
- useFormState — The orchestrator that calls this hook
- Submit & Actions — Configuring submit actions and field mapping