---
title: 'Branching survey'
description: 'A three-step survey whose follow-up step is skipped unless the answer was poor, so Next passes over it and its fields are left out of what is sent, in React and in Radiant.'
order: 5
---

import { FormExampleDemo } from '@/components/form-example-demo';
import { RendererTabs } from '@/components/renderer-tabs';
import { survey } from '@/lib/examples/branching-survey/config';

# Branching survey

A survey should not ask a happy customer what went wrong. This example is a three-step support survey: how it went, what went wrong, and a last word. The middle step belongs only to a customer who answered "Poor". For anyone else the step is skipped: Next and Previous pass over it, it counts in no progress, its fields are not validated, and what was typed in it is left out of what is sent.

## Install

For React:

```bash
pnpm add @schema-engine/forms @schema-engine/forms-react @schema-engine/layout react react-dom zod
```

The React file imports the fields that the shadcn registry installs. Run `pnpm dlx shadcn@latest add schema-engine/schema-engine/schema-form` once, as the [Contact form](/docs/examples/contact-form#install) explains.

For Radiant:

```bash
pnpm add @schema-engine/forms @schema-engine/forms-radiant @schema-engine/layout @ecopages/radiant-ui @ecopages/radiant @ecopages/jsx @ecopages/signals zod
```

Copy the files below into one folder. Radiant also needs its JSX setup and stylesheets: [Forms Radiant: installation and quick start](/docs/forms-radiant/getting-started). React needs `"jsx": "react-jsx"` in `tsconfig.json`.

## See it

Choose "Great" and select Next: the survey goes to the last step. Go back, choose "Poor" and select Next: the follow-up step opens, and Next waits for its answers. Submit the last step to see the notice.

<FormExampleDemo config={survey} />

## The config

The branch is a step-level `constraints` entry. It reads `satisfaction`, a field of an earlier step, and shows the step only when it equals `poor`. A step takes the same constraints as a field, on fields of earlier steps: the first step cannot have one, because nothing comes before it ([Constraints](/docs/forms/constraints)). While the constraint hides the step, the values kept in its fields come back if the constraint holds again, but they take no part in validation or in what is submitted. The `show_notification` entry in `actions` runs on `form_submit_success`, after the answers were sent.

```ts title="config.ts"
import type { FormConfig } from '@schema-engine/forms';

/**
 * A support survey that asks a follow-up only of a customer who had a poor experience.
 *
 * @remarks
 * The `problem` step has a constraint on a field of an earlier step. While it hides the step, Next and Previous
 * pass over it, the step counts in no progress, its fields are not validated and its values are left out of the
 * submission. The first step cannot carry a constraint, because nothing comes before it to depend on.
 */
export const survey: FormConfig = {
	id: 'support-survey',
	title: 'How did we do?',
	view: { $type: 'wizard' },
	steps: [
		{
			id: 'experience',
			title: 'Your experience',
			elements: [
				{
					$type: 'input',
					id: 'name',
					name: 'name',
					label: 'Your name',
					rules: [{ $type: 'required', message: 'Enter your name' }],
				},
				{
					$type: 'select',
					id: 'satisfaction',
					name: 'satisfaction',
					label: 'How was your support request?',
					placeholder: 'Choose one',
					options: [
						{ value: 'great', label: 'Great' },
						{ value: 'fine', label: 'Fine' },
						{ value: 'poor', label: 'Poor' },
					],
					rules: [{ $type: 'required', message: 'Choose one' }],
				},
			],
		},
		{
			id: 'problem',
			title: 'What went wrong',
			constraints: [{ field: 'satisfaction', operator: 'equals', value: 'poor', $type: 'show' }],
			elements: [
				{
					$type: 'select',
					id: 'reason',
					name: 'reason',
					label: 'What was the main problem?',
					placeholder: 'Choose one',
					options: [
						{ value: 'slow', label: 'It took too long' },
						{ value: 'unresolved', label: 'It was not solved' },
						{ value: 'rude', label: 'The agent was unhelpful' },
					],
					rules: [{ $type: 'required', message: 'Choose the main problem' }],
				},
				{
					$type: 'textarea',
					id: 'details',
					name: 'details',
					label: 'Tell us what happened',
					rows: 4,
					rules: [{ $type: 'minLength', value: 20, message: 'Use at least 20 characters' }],
				},
			],
		},
		{
			id: 'comments',
			title: 'Anything else?',
			elements: [{ $type: 'textarea', id: 'comments', name: 'comments', label: 'Comments', rows: 4 }],
		},
	],
	actions: [
		{
			id: 'thanks',
			$type: 'show_notification',
			message: 'Thanks for your answers.',
			variant: 'success',
			triggers: ['form_submit_success'],
		},
	],
};
```

## The submit handler

The values hold the fields of the steps the visitor saw. A visitor who answered "Great" sends `name`, `satisfaction` and `comments`, with no `reason` or `details`, even if they filled the follow-up step before changing their answer.

```ts title="submit.ts"
import type { FormSubmitHelpers, FormValues } from '@schema-engine/forms';

/**
 * Sends the answers as JSON. Any failure throws, which the form reports as a failed submission.
 *
 * @remarks
 * The values hold the fields of the steps the visitor saw: the follow-up step's fields are absent when it was skipped.
 */
export async function sendAnswers(values: FormValues, { fetch }: FormSubmitHelpers): Promise<void> {
	const response = await fetch('/api/surveys', {
		method: 'POST',
		headers: { 'Content-Type': 'application/json' },
		body: JSON.stringify(values),
	});
	if (!response.ok) throw new Error(`The server answered ${response.status}.`);
}
```

## The renderer

Radiant ships the wizard recipe and the elements. React ships neither: its tab registers the installed fields and a recipe of its own, the one of the [Registration wizard](/docs/examples/registration-wizard) with the submit button's label changed. `FormRenderer` calls `onSubmit` with the values once the last step passes, and the file calls `onSent` when `sendAnswers` returns. Both engines register `NotificationActionRegistration`, the package's own, which only logs the message because the package cannot know how an app shows one; the demos on this page register one that shows a toast, and [Submit, notify and reset](/docs/examples/submit-notify-reset) registers one of your own.

<RendererTabs name="branching-survey">
<div data-renderer="react">

```tsx title="wizard.react.tsx"
import { defineRecipeSpec } from '@schema-engine/forms/recipes';
import { WizardActionsConfigSchema } from '@schema-engine/forms/recipes/built-in';
import { useViewContext } from '@schema-engine/forms-react/hooks';
import { defineRecipe, type FormShellProps } from '@schema-engine/forms-react/primitives';

const wizardSpec = defineRecipeSpec({
	$type: 'wizard',
	kind: 'wizard',
	slotSchemas: { actions: WizardActionsConfigSchema },
	metadata: { name: 'Wizard', category: 'wizard' },
});

/** The shell lays out what the recipe draws: the fields of the current step, then the controls. */
function Shell({ slots }: FormShellProps) {
	return (
		<div>
			{slots.content}
			{slots.actions}
		</div>
	);
}

/**
 * Previous, the step counter, and Next or the submit button.
 *
 * @remarks
 * Next and Submit carry distinct keys so React replaces the button instead of turning Next into a submit button
 * while its click is still being handled, which would submit the form on arriving at the last step.
 */
function Controls() {
	const { navigation, form, steps, currentStepIndex } = useViewContext();
	return (
		<div>
			<button type="button" onClick={navigation.goToPreviousStep} disabled={navigation.isFirstStep}>
				Previous
			</button>
			<span aria-live="polite">
				Step {currentStepIndex + 1} of {steps.length}
			</span>
			{navigation.isLastStep ? (
				<button key="submit" type="submit">
					{form.isSubmitting ? 'Sending' : 'Send answers'}
				</button>
			) : (
				<button key="next" type="button" onClick={navigation.goToNextStep} disabled={!navigation.canGoNext}>
					Next
				</button>
			)}
		</div>
	);
}

export const WizardRecipe = defineRecipe(wizardSpec, { shell: Shell, slots: { actions: Controls } });
```

```tsx title="branching-survey.react.tsx"
import { createFormEngine, NotificationActionRegistration } from '@schema-engine/forms';
import { FormRenderer } from '@schema-engine/forms-react';
import { survey } from './config';
import { sendAnswers } from './submit';
import { schemaFormRegistrations } from '@/components/schema-engine/forms/registrations';
import { WizardRecipe } from './wizard.react';

export const engine = createFormEngine({
	elements: schemaFormRegistrations,
	recipes: [WizardRecipe],
	actions: [NotificationActionRegistration],
});

/** Renders the survey and calls `onSent` once the server has the answers. */
export function Survey({ onSent }: { onSent: () => void }) {
	return (
		<FormRenderer
			engine={engine}
			config={survey}
			onSubmit={async (values, helpers) => {
				await sendAnswers(values, helpers);
				onSent();
			}}
		/>
	);
}
```

</div>
<div data-renderer="radiant">

```tsx title="branching-survey.radiant.tsx"
import { NotificationActionRegistration } from '@schema-engine/forms';
import { createRadiantFormEngine, FormRenderer, schemaFormRegistrations } from '@schema-engine/forms-radiant';
import { survey } from './config';
import { sendAnswers } from './submit';

export const engine = createRadiantFormEngine({
	elements: schemaFormRegistrations,
	actions: [NotificationActionRegistration],
});

/** Renders the survey and calls `onSent` once the server has the answers. */
export function Survey({ onSent }: { onSent: () => void }) {
	return (
		<FormRenderer
			engine={engine}
			config={survey}
			onSubmit={async (values, helpers) => {
				await sendAnswers(values, helpers);
				onSent();
			}}
		/>
	);
}
```

</div>
</RendererTabs>

## What the tests check

The example's tests render both files in a browser. A good answer goes from the first step to the last and sends no follow-up fields. A poor answer opens the follow-up step, where Next names each failing rule. Changing the answer back after filling the follow-up step drops what was typed there from the submission. A failed send does not call `onSent`. The unit test checks the `show_notification` entry of the config in both engines, so a config that names an action an engine did not register fails.

## Take it further

- Branch on a field inside a step instead: the ticket field of the [Contact form](/docs/examples/contact-form).
- Let the visitor jump between steps: [Wizard](/docs/forms/wizard).
- Show the answer to a failed send on the form: [Server errors](/docs/forms/server-errors).
