/** * Custom Error Display Example * * Demonstrates: * - Custom error component * - Error summary at top of form * - Toast notifications for errors * - Inline vs summary error display * - Accessible error announcements * - Icon-based error styling */ import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' import { useEffect, useState } from 'react' const formSchema = z.object({ username: z.string().min(3, 'Username must be at least 3 characters'), email: z.string().email('Invalid email address'), password: z.string().min(8, 'Password must be at least 8 characters'), age: z.number().min(18, 'You must be at least 18 years old'), }) type FormData = z.infer /** * Custom Error Component */ function FormError({ message, icon = true }: { message: string; icon?: boolean }) { return (
{icon && ( )} {message}
) } /** * Error Summary Component */ function ErrorSummary({ errors }: { errors: Record }) { const errorEntries = Object.entries(errors).filter(([key, value]) => value?.message) if (errorEntries.length === 0) return null return (

{errorEntries.length} {errorEntries.length === 1 ? 'Error' : 'Errors'} Found

    {errorEntries.map(([field, error]) => (
  • {field}: {error.message}
  • ))}
) } /** * Toast Notification for Errors */ function ErrorToast({ message, onClose }: { message: string; onClose: () => void }) { useEffect(() => { const timer = setTimeout(onClose, 5000) return () => clearTimeout(timer) }, [onClose]) return (

Validation Error

{message}

) } /** * Form with Custom Error Display */ export function CustomErrorDisplayForm() { const { register, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(formSchema), defaultValues: { username: '', email: '', password: '', age: 18, }, }) const [toastMessage, setToastMessage] = useState(null) const onSubmit = async (data: FormData) => { console.log('Form data:', data) setToastMessage('Form submitted successfully!') } const onError = (errors: any) => { // Show toast on validation error const errorCount = Object.keys(errors).length setToastMessage(`Please fix ${errorCount} error${errorCount > 1 ? 's' : ''} before submitting`) } return (

Registration Form

{/* Error Summary */} {/* Username */}
{errors.username && ( )}
{/* Email */}
{errors.email && ( )}
{/* Password */}
{errors.password && ( )}
{/* Age */}
{errors.age && ( )}
{/* Toast Notification */} {toastMessage && ( setToastMessage(null)} /> )}
) } /** * Alternative: Grouped Error Display */ export function GroupedErrorDisplayForm() { const { register, handleSubmit, formState: { errors } } = useForm({ resolver: zodResolver(formSchema), }) return (
console.log(data))} className="max-w-2xl mx-auto space-y-6">

Grouped Error Display

{/* All errors in single container */} {Object.keys(errors).length > 0 && (

Please correct the following:

{Object.entries(errors).map(([field, error]) => (
{field}: {error.message}
))}
)} {/* Form fields without individual error messages */}
) }