Lesson 7 of 8
Multi-Step Forms
Multi-step forms present complex data entry across multiple screens, maintaining state between steps, validating per-step, and showing progress. The key is validating only the current step's fields before advancing, while keeping all form state accessible in the final submit.
Multi-Step Form with Per-Step Validation
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
// Define all steps' schemas
const step1Schema = z.object({
firstName: z.string().min(1, 'Required'),
lastName: z.string().min(1, 'Required'),
email: z.string().email('Valid email required'),
});
const step2Schema = z.object({
company: z.string().optional(),
role: z.enum(['developer', 'designer', 'manager', 'other']),
experience: z.enum(['<1', '1-3', '3-5', '5+']),
});
const step3Schema = z.object({
plan: z.enum(['free', 'pro', 'enterprise']),
billing: z.enum(['monthly', 'annual']).optional(),
agreeToTerms: z.boolean().refine(v => v, 'Required'),
});
// Full schema = all steps combined
const fullSchema = step1Schema.merge(step2Schema).merge(step3Schema);
type FullFormData = z.infer<typeof fullSchema>;
const STEPS = [
{ title: 'Personal Info', schema: step1Schema, fields: ['firstName', 'lastName', 'email'] as const },
{ title: 'Professional', schema: step2Schema, fields: ['company', 'role', 'experience'] as const },
{ title: 'Plan', schema: step3Schema, fields: ['plan', 'billing', 'agreeToTerms'] as const },
];
function MultiStepSignup() {
const [currentStep, setCurrentStep] = useState(0);
const [isComplete, setIsComplete] = useState(false);
const form = useForm<FullFormData>({
resolver: zodResolver(fullSchema),
mode: 'onBlur',
defaultValues: {
firstName: '', lastName: '', email: '',
company: '', role: 'developer', experience: '<1',
plan: 'free', agreeToTerms: false,
},
});
const { register, trigger, handleSubmit, formState: { errors, isSubmitting }, watch } = form;
async function nextStep() {
// Only validate the CURRENT step's fields before advancing
const fieldsToValidate = STEPS[currentStep].fields;
const isValid = await trigger(fieldsToValidate);
if (isValid) setCurrentStep(s => s + 1);
}
async function onSubmit(data: FullFormData) {
await createAccount(data);
setIsComplete(true);
}
const selectedPlan = watch('plan');
if (isComplete) {
return (
<div style={{ textAlign: 'center', padding: 40 }}>
<p style={{ fontSize: 48 }}>๐</p>
<h2>Account Created!</h2>
<p>Check your email to verify your account.</p>
</div>
);
}
return (
<div style={{ maxWidth: 480, margin: '0 auto', padding: 24, fontFamily: 'system-ui', color: 'white' }}>
{/* Progress steps */}
<div style={{ display: 'flex', gap: 4, marginBottom: 32 }}>
{STEPS.map((step, i) => (
<div key={i} style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6 }}>
<div style={{
width: 32, height: 32, borderRadius: '50%', display: 'flex', alignItems: 'center', justifyContent: 'center',
background: i < currentStep ? '#10b981' : i === currentStep ? '#229c6b' : '#334155',
fontWeight: 700, fontSize: 13,
}}>
{i < currentStep ? 'โ' : i + 1}
</div>
<span style={{ fontSize: 11, color: i === currentStep ? '#e2e8f0' : '#64748b' }}>{step.title}</span>
{i < STEPS.length - 1 && (
<div style={{ position: 'absolute', left: `${(i * 100 / STEPS.length) + 16}%`, top: 16, width: `${100 / STEPS.length - 8}%`, height: 2, background: i < currentStep ? '#10b981' : '#334155' }} />
)}
</div>
))}
</div>
<form onSubmit={handleSubmit(onSubmit)}>
{/* Step 1 */}
{currentStep === 0 && (
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<h2 style={{ margin: 0 }}>Personal Information</h2>
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
<div>
<label style={{ fontSize: 13, color: '#94a3b8', display: 'block', marginBottom: 4 }}>First Name *</label>
<input {...register('firstName')} style={{ width: '100%', padding: '8px 12px', background: '#1e293b', border: `1px solid ${errors.firstName ? '#ef4444' : '#334155'}`, color: 'white', borderRadius: 8, boxSizing: 'border-box' as const }} />
{errors.firstName && <p style={{ color: '#f87171', fontSize: 12, margin: '2px 0 0' }}>{errors.firstName.message}</p>}
</div>
<div>
<label style={{ fontSize: 13, color: '#94a3b8', display: 'block', marginBottom: 4 }}>Last Name *</label>
<input {...register('lastName')} style={{ width: '100%', padding: '8px 12px', background: '#1e293b', border: `1px solid ${errors.lastName ? '#ef4444' : '#334155'}`, color: 'white', borderRadius: 8, boxSizing: 'border-box' as const }} />
{errors.lastName && <p style={{ color: '#f87171', fontSize: 12, margin: '2px 0 0' }}>{errors.lastName.message}</p>}
</div>
</div>
<div>
<label style={{ fontSize: 13, color: '#94a3b8', display: 'block', marginBottom: 4 }}>Email *</label>
<input {...register('email')} type="email" style={{ width: '100%', padding: '8px 12px', background: '#1e293b', border: `1px solid ${errors.email ? '#ef4444' : '#334155'}`, color: 'white', borderRadius: 8, boxSizing: 'border-box' as const }} />
{errors.email && <p style={{ color: '#f87171', fontSize: 12, margin: '2px 0 0' }}>{errors.email.message}</p>}
</div>
</div>
)}
{/* Step 2 */}
{currentStep === 1 && (
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<h2 style={{ margin: 0 }}>Professional Details</h2>
<div>
<label style={{ fontSize: 13, color: '#94a3b8', display: 'block', marginBottom: 4 }}>Company (optional)</label>
<input {...register('company')} style={{ width: '100%', padding: '8px 12px', background: '#1e293b', border: '1px solid #334155', color: 'white', borderRadius: 8, boxSizing: 'border-box' as const }} />
</div>
<div>
<label style={{ fontSize: 13, color: '#94a3b8', display: 'block', marginBottom: 4 }}>Role *</label>
<select {...register('role')} style={{ width: '100%', padding: '8px 12px', background: '#1e293b', border: '1px solid #334155', color: 'white', borderRadius: 8 }}>
<option value="developer">Developer</option>
<option value="designer">Designer</option>
<option value="manager">Manager</option>
<option value="other">Other</option>
</select>
</div>
<div>
<label style={{ fontSize: 13, color: '#94a3b8', display: 'block', marginBottom: 8 }}>Years of Experience *</label>
<div style={{ display: 'flex', gap: 8 }}>
{(['<1', '1-3', '3-5', '5+'] as const).map(exp => (
<label key={exp} style={{ flex: 1, border: '1px solid #334155', borderRadius: 8, padding: '8px 0', textAlign: 'center', cursor: 'pointer', fontSize: 14 }}>
<input {...register('experience')} type="radio" value={exp} style={{ display: 'none' }} />
{exp} yrs
</label>
))}
</div>
</div>
</div>
)}
{/* Step 3 */}
{currentStep === 2 && (
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<h2 style={{ margin: 0 }}>Choose Your Plan</h2>
{(['free', 'pro', 'enterprise'] as const).map(plan => (
<label key={plan} style={{ display: 'flex', alignItems: 'center', gap: 12, border: `2px solid ${selectedPlan === plan ? '#229c6b' : '#334155'}`, borderRadius: 10, padding: 16, cursor: 'pointer' }}>
<input {...register('plan')} type="radio" value={plan} />
<div>
<p style={{ margin: 0, fontWeight: 700, textTransform: 'capitalize' }}>{plan}</p>
<p style={{ margin: 0, fontSize: 13, color: '#94a3b8' }}>
{plan === 'free' ? 'Free forever, limited features' : plan === 'pro' ? '$9/mo ยท All features' : 'Custom pricing ยท Enterprise SLA'}
</p>
</div>
</label>
))}
<label style={{ display: 'flex', alignItems: 'center', gap: 10, fontSize: 13 }}>
<input type="checkbox" {...register('agreeToTerms')} />
I agree to the <a href="/terms" style={{ color: '#60a5fa' }}>Terms of Service</a> and <a href="/privacy" style={{ color: '#60a5fa' }}>Privacy Policy</a> *
</label>
{errors.agreeToTerms && <p style={{ color: '#f87171', fontSize: 12 }}>{errors.agreeToTerms.message}</p>}
</div>
)}
{/* Navigation buttons */}
<div style={{ display: 'flex', justifyContent: 'space-between', marginTop: 28, gap: 12 }}>
{currentStep > 0 ? (
<button type="button" onClick={() => setCurrentStep(s => s - 1)} style={{ flex: 1, background: 'transparent', border: '1px solid #334155', color: '#94a3b8', padding: '10px', borderRadius: 8, cursor: 'pointer', fontWeight: 600 }}>
โ Back
</button>
) : <div style={{ flex: 1 }} />}
{currentStep < STEPS.length - 1 ? (
<button type="button" onClick={nextStep} style={{ flex: 1, background: '#229c6b', border: 'none', color: 'white', padding: '10px', borderRadius: 8, cursor: 'pointer', fontWeight: 600 }}>
Next โ
</button>
) : (
<button type="submit" disabled={isSubmitting} style={{ flex: 1, background: '#10b981', border: 'none', color: 'white', padding: '10px', borderRadius: 8, cursor: 'pointer', fontWeight: 600 }}>
{isSubmitting ? 'Creating...' : 'Create Account'}
</button>
)}
</div>
</form>
</div>
);
}
export default MultiStepSignup;What This Demonstrates
- One useForm instance for all steps - form state persists across step transitions without extra state management
- Per-step validation with `trigger()` - validates only the current step's fields array before advancing
- Merged Zod schemas - each step has its own schema for clear validation; merged into one for final submission type
- watch() for reactive UI - plan selection highlighted based on watched value without extra state
- Progress indicator - step completion tracked by comparing `currentStep` index
- `handleSubmit` only fires on the final step submit button - previous steps use type='button' with next()
Tip
Tip
Practice MultiStep Forms in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Multi-Step Forms
Practice Task
Note
Practice Task - (1) Write a working example of MultiStep Forms from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with MultiStep Forms is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- Multi-step forms present complex data entry across multiple screens, maintaining state between steps, validating per-step, and showing progress.
- One useForm instance for all steps - form state persists across step transitions without extra state management
- Per-step validation with `trigger()` - validates only the current step's fields array before advancing
- Merged Zod schemas - each step has its own schema for clear validation; merged into one for final submission type
Finished this lesson?