Skip to content
LogoLogo

How to run code on a form submission

This guide shows you how to act on a step submission. Pass your callbacks to useStep() in React, or to getStep() in Svelte.

What the SDK does on submit

  1. It collects the field values from the HTML form.
  2. It runs each onBeforeSubmit callback, one after another.
  3. It checks each required field and each validation rule.
  4. It commits the responses to the experience state.
  5. It fires form_submit_success or form_submit_error on the event bus.
  6. It runs your onSuccess or onError callback.
  7. It moves to the next step.

Change the data before it is stored

const { form } = useStep({
  onBeforeSubmit: async (data) => ({
    ...data,
    'email-address': String(data['email-address']).toLowerCase(),
  }),
})

Act on the outcome

const { form } = useStep({
  onSuccess: ({ data }) => analytics.track('lead_captured', data),
  onError: (errors) => console.error('Validation errors:', errors),
})

A callback belongs to the component that passes it, and the SDK releases it when that component unmounts. An onSuccess callback that throws cannot stop the flow. The SDK reports the failure as form_submit_callback_error.

Choose the next step yourself

Call stopNavigation() inside onSuccess, then move where you want:

const experience = useExperience()
 
const { form } = useStep({
  onSuccess: ({ data, stopNavigation }) => {
    stopNavigation()
    if (data['plan-type'] === 'enterprise') {
      experience.goToStepById('enterprise-contact')
    } else {
      experience.goToNextStep()
    }
  },
})

goToStepById() takes a step id. goToStepByKey() takes the key of the step. When no step matches, the call does nothing.