# Multi-step lead form with conditional routing and a webhook

URL: https://docs.forms.saastro.io/docs/recipes/multi-step-webhook
> A lead-qualification wizard that routes the second step by answer, shows a follow-up field conditionally, and posts every lead to a webhook.

# Multi-step lead form with conditional routing and a webhook

## What it combines

A lead-qualification wizard that combines three features:

- **Multi-step routing** — the second step depends on what the visitor is interested in ([Multi-Step guide](/docs/multi-step))
- **Conditional field visibility** — a follow-up field appears only when "Other" is selected ([Conditional Logic guide](/docs/conditional-logic))
- **Submit actions** — a webhook captures every lead, and a second, conditional webhook fires only for demo requests ([Submit & Actions guide](/docs/submit))

```tsx
import { FormBuilder } from '@saastro/forms';

export function createConfig({ crmUrl, salesUrl }: { crmUrl: string; salesUrl: string }) {
  return (
    FormBuilder.create('lead-qualifier')
      // Manual grid: every field declares its own span — fields
      // without .columns() default to a single column (1 of 12)
      .layout('manual')
      .columns(12)

      // Step 1 — who they are
      .addField('name', (f) =>
        f.type('text').label('Full Name').required().minLength(2).columns({ default: 12, md: 6 }),
      )
      .addField('email', (f) =>
        f.type('email').label('Work Email').required().email().columns({ default: 12, md: 6 }),
      )
      .addField('interest', (f) =>
        f
          .type('button-radio')
          .label('What are you looking for?')
          .required()
          .options([
            { value: 'demo', label: 'Book a demo' },
            { value: 'pricing', label: 'Pricing information' },
          ])
          .columns({ default: 12 }),
      )

      // Step 2a — demo route
      .addField('teamSize', (f) =>
        f
          .type('select')
          .label('Team Size')
          .required()
          .options([
            { value: '1-10', label: '1-10' },
            { value: '11-50', label: '11-50' },
            { value: '51+', label: '51+' },
          ])
          .columns({ default: 12, md: 6 }),
      )
      .addField('useCase', (f) =>
        f
          .type('select')
          .label('Primary Use Case')
          .required()
          .options([
            { value: 'marketing', label: 'Marketing' },
            { value: 'sales', label: 'Sales' },
            { value: 'other', label: 'Other' },
          ])
          .columns({ default: 12, md: 6 }),
      )
      .addField('otherUseCase', (f) =>
        f
          .type('text')
          .label('Tell us more')
          .optional()
          // Hidden unless "Other" is selected. Keep conditionally hidden
          // fields optional — hidden fields are still validated and submitted.
          .hidden({
            conditions: [{ field: 'useCase', operator: 'notEquals', value: 'other' }],
            operator: 'AND',
          })
          .columns({ default: 12 }),
      )

      // Step 2b — pricing route
      .addField('budget', (f) =>
        f
          .type('select')
          .label('Monthly Budget')
          .required()
          .options([
            { value: '<500', label: 'Under $500' },
            { value: '500-2000', label: '$500 – $2,000' },
            { value: '>2000', label: 'Over $2,000' },
          ])
          .columns({ default: 12 }),
      )

      // Step 3 — wrap-up
      .addField('message', (f) =>
        f.type('textarea').label('Anything else?').optional().rows(4).columns({ default: 12 }),
      )

      // Steps + conditional routing: first matching condition wins,
      // otherwise defaultNext is used
      .addStep('about-you', ['name', 'email', 'interest'], {
        defaultNext: 'pricing-details',
        next: [
          {
            conditions: [{ field: 'interest', operator: 'equals', value: 'demo' }],
            operator: 'AND',
            target: 'demo-details',
          },
        ],
      })
      .addStep('demo-details', ['teamSize', 'useCase', 'otherUseCase'], { defaultNext: 'finish' })
      .addStep('pricing-details', ['budget'], { defaultNext: 'finish' })
      .addStep('finish', ['message'])
      .initialStep('about-you')

      // Override the default button labels (Submit / Next → / ← Back)
      .buttons({
        align: 'between',
        submit: { type: 'submit', label: 'Send' },
        next: { type: 'next', label: 'Continue' },
        back: { type: 'back', label: 'Back', variant: 'outline' },
      })

      // Webhook 1 — captures every lead (default JSON payload of all values)
      .submitAction(
        'crm-webhook',
        {
          type: 'webhook',
          name: 'CRM Webhook',
          url: crmUrl,
          secret: 'your-webhook-secret', // optional HMAC-SHA256 → X-Webhook-Signature header
        },
        'onSubmit',
      )

      // Webhook 2 — fires only for demo requests, after the first one
      .submitAction(
        'notify-sales',
        {
          type: 'webhook',
          name: 'Notify Sales',
          url: salesUrl,
          payloadTemplate: JSON.stringify({
            event: 'demo_requested',
            name: '{{name}}',
            email: '{{email}}',
            teamSize: '{{teamSize}}',
          }),
        },
        'onSubmit',
        {
          condition: { field: 'interest', operator: 'equals', value: 'demo' },
          order: 2,
        },
      )

      .submitExecution({ mode: 'sequential', stopOnFirstError: true })

      // Custom success/error messages (defaults: '✅ Thank you!' / a generic error)
      .successMessage("Thanks! We'll be in touch within one business day.")
      .errorMessage('Something went wrong — please try again.')

      .build()
  );
}
```

## The code

The **Code** tab above is the whole recipe: `createConfig({ crmUrl, salesUrl })` returns the form config. Render it with the components you already inject:

```tsx
import { Form } from '@saastro/forms';
import { createConfig } from './recipe';

// Vite zero-config component discovery — for Next.js and other
// non-Vite bundlers, see the "Using @saastro/forms without Vite" recipe.
const uiComponents = import.meta.glob('@/components/ui/*.tsx', { eager: true });

const config = createConfig({
  crmUrl: 'https://hooks.example.com/catch/lead',
  salesUrl: 'https://hooks.example.com/catch/demo-request',
});

export function LeadForm() {
  return <Form config={config} components={uiComponents} />;
}
```

The flow:

```
about-you → demo-details    → finish     (if interest = "demo")
about-you → pricing-details → finish     (otherwise)
```

## How it works

- **Routing** — each step's `next` conditions are evaluated in order against the current values; the first match wins, otherwise `defaultNext` applies. The Back button follows the visited-step history, so a user who took the demo route goes back through the demo route.
- **Conditional visibility** — `otherUseCase` renders only when `useCase` is `'other'`. A conditionally hidden field stays registered: it is still validated when its step advances and its value is still submitted, which is why it's marked `.optional()`. See [Conditional Logic](/docs/conditional-logic).
- **Submit actions** — because at least one action has an `onSubmit` trigger, these actions are the entire submission. They run sequentially by `order` (default `0`). The `notify-sales` action's `condition` is checked at submit time; when it isn't met the action is skipped and counts as successful.
- **Failures** — if any executed action fails, the submission fails: the form shows its error state with your `errorMessage`. `stopOnFirstError` only controls whether the _remaining_ actions still run. Details in [Submit & Actions](/docs/submit).
- **Custom copy** — button labels and the success/error messages have built-in English defaults (`Submit`, `✅ Thank you!`, …); the recipe overrides them to match its own tone. For multi-language forms, use a locale overlay — or flip all package defaults at once with `setDefaultMessages` — see the [i18n guide](/docs/i18n).

## Caveats

- **Browser caveats** — webhook actions run client-side, so the endpoint must accept cross-origin requests (Zapier/Make/n8n catch hooks do), and any `secret` you configure ships in your JavaScript bundle — treat it as an integrity check, not as a credential.
- **Placeholders** — `{{placeholders}}` in `payloadTemplate` match word characters only (letters, digits, underscore), so use field names like `teamSize`, not `team-size`.
- **Hidden fields are still submitted** — keep conditionally hidden fields optional, as `otherUseCase` is.

## Variations

- Swap the webhooks for an `http` action when you control the backend ([Submit & Actions](/docs/submit)).
- Add a third route by adding another entry to the first step's `next` array.
- Pair it with [Using @saastro/forms without Vite](/docs/recipes/nextjs-without-vite) when your app is not on Vite.

## Related

- [Multi-Step Forms](/docs/multi-step)
- [Conditional Logic](/docs/conditional-logic)
- [Submit & Actions](/docs/submit)
