# LambdaCurry Forms - Complete Implementation Guide for LLMs This comprehensive guide covers everything needed to implement forms using the `@lambdacurry/forms` remix-hook-form components, including the new FormError component for form-level error handling. This documentation is specifically designed for LLMs to understand all features, patterns, and best practices. ## Core Architecture Overview The library provides **form-aware wrapper components** in the `remix-hook-form` directory that automatically integrate with React Router forms and Remix Hook Form context. These components eliminate boilerplate while maintaining full customization capabilities. ### Key Principle: Zero Boilerplate Form Integration - Components automatically access form context via `useRemixFormContext()` - No need to manually pass `control` props - Automatic error handling and validation display - Built-in accessibility features ## Form-Level Error Handling with FormError The `FormError` component provides standardized form-level error handling, complementing the existing field-level error system. ### FormError Component Usage ```typescript // Import form-aware FormError component import { FormError } from '@lambdacurry/forms'; // Basic usage - looks for errors._form by default // Custom error key // With custom styling and placement // With custom component override ``` ### Server Action Pattern for Form-Level Errors ```typescript export const action = async ({ request }: ActionFunctionArgs) => { const { data, errors } = await getValidatedFormData( request, zodResolver(formSchema) ); // Return field-level validation errors if (errors) { return { errors }; } // Business logic validation try { await processForm(data); return { message: 'Success!' }; } catch (error) { // Return form-level error using _form key return { errors: { _form: { message: 'Unable to process form. Please try again.' } } }; } }; ``` ### Error Hierarchy Guidelines **Field-Level Errors (use FormMessage automatically in form components):** - Validation errors: "Email is required", "Password too short" - Format errors: "Invalid email format" - Field-specific business rules: "Username already taken" **Form-Level Errors (use FormError component):** - Server errors: "Server temporarily unavailable" - Authentication failures: "Invalid credentials" - Network issues: "Connection timeout" - General business logic: "Account suspended" - Rate limiting: "Too many attempts, try again later" ## Basic Form Setup Pattern ## Import Structure The `@lambdacurry/forms` library follows a clear import structure: ### Form-Aware Components (from `@lambdacurry/forms`) These components automatically integrate with React Router forms and Remix Hook Form context: - `TextField` - Form-aware text input with automatic validation - `Textarea` - Form-aware textarea with automatic validation - `Checkbox` - Form-aware checkbox with automatic validation - `Switch` - Form-aware switch/toggle with automatic validation - `RadioGroup` - Form-aware radio group with automatic validation - `RadioGroupItem` - Individual radio items for RadioGroup - `DatePicker` - Form-aware date picker with automatic validation - `DropdownMenuSelect` - Form-aware dropdown select with automatic validation - `OtpInput` - Form-aware OTP/PIN input with automatic validation - `FormError` - Component for displaying form-level errors - `FormLabel`, `FormControl`, `FormDescription`, `FormMessage` - Form field components - `useFormField` - Hook for accessing form field context - Data table components: `DataTableRouterForm`, `DataTableRouterToolbar`, etc. ### UI Components (from `@lambdacurry/forms/ui`) These are the underlying UI components without form integration: - `Button` - Button component with variants - `Calendar` - Calendar component for date selection - `Badge` - Badge/chip component for labels - `Dialog` - Modal dialog component - `Popover` - Popover/tooltip component - `Select` - Basic select dropdown - `Separator` - Visual separator/divider - `Slider` - Range slider component - `Table` - Table components for data display - `Tabs` - Tab navigation component - `Command` - Command palette/search component - Field variants: `CheckboxField`, `TextareaField`, `TextField`, `SwitchField`, etc. - And many more UI primitives ## Component Reference ### Complete List of Form-Aware Components (`@lambdacurry/forms`) ```typescript // Form input components (automatically integrate with form context) import { TextField, // Text input with validation Textarea, // Multi-line text input with validation Checkbox, // Checkbox with validation Switch, // Toggle switch with validation RadioGroup, // Radio button group with validation RadioGroupItem, // Individual radio button DatePicker, // Date selection with validation DropdownMenuSelect, // Dropdown select with validation OtpInput, // OTP/PIN input with validation // Form structure components FormError, // Form-level error display FormLabel, // Form field labels FormControl, // Form field wrapper FormDescription, // Form field descriptions FormMessage, // Form field error messages useFormField, // Hook for form field context // Data table components DataTableRouterForm, DataTableRouterToolbar, useDataTableUrlState, } from '@lambdacurry/forms'; ``` ### Complete List of UI Components (`@lambdacurry/forms/ui`) ```typescript // Basic UI components (no form integration) import { // Buttons and actions Button, // Button with variants // Form inputs (non-form-aware versions) CheckboxField, // Checkbox without form integration TextareaField, // Textarea without form integration TextField, // Text input without form integration SwitchField, // Switch without form integration RadioGroupField, // Radio group without form integration DatePickerField, // Date picker without form integration DropdownMenuSelectField, // Dropdown without form integration OtpInputField, // OTP input without form integration FormErrorField, // Error display without form integration // Layout and navigation Dialog, // Modal dialogs Popover, // Popover/tooltips Tabs, // Tab navigation Separator, // Visual dividers // Data display Table, // Table components Badge, // Labels and badges Calendar, // Calendar component // Form primitives Label, // Basic labels Select, // Basic select dropdown Slider, // Range slider Command, // Command palette // Data table system DataTable, // Data table components DataTableFilter, // Table filtering // Utilities DebouncedInput, // Debounced input } from '@lambdacurry/forms/ui'; ``` ### 1. Required Imports ```typescript import { zodResolver } from '@hookform/resolvers/zod'; import { RemixFormProvider, useRemixForm, getValidatedFormData } from 'remix-hook-form'; import { z } from 'zod'; import { useFetcher, type ActionFunctionArgs } from 'react-router'; // Import form-aware components from main package import { TextField, Checkbox, FormError } from '@lambdacurry/forms'; // Import UI components from /ui subpath import { Button } from '@lambdacurry/forms/ui'; ``` ### 2. Zod Schema Definition ```typescript const formSchema = z.object({ username: z.string().min(3, 'Username must be at least 3 characters'), email: z.string().email('Invalid email address'), terms: z.boolean().refine(val => val === true, 'You must accept the terms'), age: z.coerce.number().min(18, 'Must be at least 18 years old'), }); type FormData = z.infer; ``` ### 3. Complete Login Form Example with FormError ```typescript const LoginForm = () => { const fetcher = useFetcher<{ message?: string; errors?: Record }>(); const methods = useRemixForm({ resolver: zodResolver(formSchema), defaultValues: { email: '', password: '', }, fetcher, submitConfig: { action: '/login', method: 'post', }, }); const isSubmitting = fetcher.state === 'submitting'; return (

Sign In

{fetcher.data?.message && (

{fetcher.data.message}

)}
); }; ``` ### 4. General Form Component Setup ```typescript const MyFormComponent = () => { const fetcher = useFetcher<{ message: string; errors?: Record }>(); const methods = useRemixForm({ resolver: zodResolver(formSchema), defaultValues: { username: '', email: '', terms: false, age: undefined, }, fetcher, submitConfig: { action: '/', method: 'post', }, }); return ( {fetcher.data?.message &&

{fetcher.data.message}

}
); }; ``` ### 5. Server Action Handler with FormError Support ```typescript export const action = async ({ request }: ActionFunctionArgs) => { const { data, errors } = await getValidatedFormData( request, zodResolver(formSchema) ); if (errors) { return { errors }; } // Business logic validation try { const user = await authenticateUser(data.email, data.password); return { message: 'Login successful!', redirectTo: '/dashboard' }; } catch (error) { // Return form-level error using _form key return { errors: { _form: { message: 'Invalid credentials. Please try again.' } } }; } }; ``` ## Available Form Components ### TextField Component ```typescript ``` ### Textarea Component ```typescript