Form Controller
A lightweight yet powerful form management system for building dynamic, multi-step forms with advanced validation capabilities.
Examples
Explore interactive examples of Form Controller in action.
Single-Step Form Example
Beginner-friendly example
A basic form with field validation and submission handling. Perfect starting point for learning Form Controller basics.
Debug Information
{ "contact": { "name": "", "email": "" } }
Single-Step Form Example
A basic form with field validation and submission handling. Perfect starting point for learning Form Controller basics.
Basic Form Setup
The simplest form setup uses a single step with basic field validation:
1// Define your form configuration2const formConfig = {3 steps: [4 {5 id: 'contact',6 title: 'Contact Information',7 fields: {8 name: {9 id: 'name',10 value: '',11 label: 'Full Name',12 type: 'text',13 required: true,14 },15 email: {16 id: 'email',17 value: '',18 label: 'Email Address',19 type: 'email',20 required: true,21 validation: {22 pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/23 }24 }25 }26 }27 ]28};2930// Initialize the form controller31const { state, methods } = useUplink(32 () => FormController(formConfig),33 { trackBindings: "all" }34);
Form Component Implementation
Implement your form with fields and error handling:
1<form onSubmit={handleSubmit}>2 <div>3 <label>4 Full Name <span className="text-red-500">*</span>5 </label>6 <input7 type="text"8 value={state.formData.contact?.name || ""}9 onChange={(e) => methods.updateField('contact', 'name', e.target.value)}10 />11 {state.fieldErrors.contact?.name && (12 <div className="error">13 {state.fieldErrors.contact.name}14 </div>15 )}16 </div>1718 <div>19 <label>20 Email Address <span className="text-red-500">*</span>21 </label>22 <input23 type="email"24 value={state.formData.contact?.email || ""}25 onChange={(e) => methods.updateField('contact', 'email', e.target.value)}26 />27 {state.fieldErrors.contact?.email && (28 <div className="error">29 {state.fieldErrors.contact.email}30 </div>31 )}32 </div>3334 <button type="submit">Submit</button>35</form>
Form Submission Handling
Implement form submission with validation handling:
1// Handle form submission2const handleSubmit = (e) => {3 e.preventDefault();4 const result = methods.submitForm();56 if (result.success) {7 console.log('Form data:', result.data);8 // Process the valid form data9 // For example: make an API call, show success message, etc.10 alert("Form submitted successfully!");11 } else {12 // Handle validation errors13 alert("Please fix the errors before submitting.");14 }15};
Key Benefits
This simple form example demonstrates:
- Basic form configuration and setup
- Field validation with pattern matching
- Form submission and data handling
- Error reporting and validation feedback
- Simple state management with minimal code
Complete Code Example
1import React from "react";2import { useUplink } from " @uplink-protocol/react";3import { FormController } from "@uplink-protocol/form-controller";45function DynamicFormStepper() {6 // Define your form configuration7 const formConfig = {8 steps: [9 {10 id: 'contact',11 title: 'Contact Information',12 fields: {13 name: {14 id: 'name',15 value: '',16 label: 'Full Name',17 type: 'text',18 required: true,19 },20 email: {21 id: 'email',22 value: '',23 label: 'Email Address',24 type: 'email',25 required: true,26 validation: {27 pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/28 }29 }30 }31 }32 ]33 };3435 // Initialize the form controller with useUplink36 const { state, methods } = useUplink(37 () => FormController(formConfig),38 { trackBindings: "all" }39 );4041 // Handle form submission42 const handleSubmit = (e) => {43 e.preventDefault();44 const result = methods.submitForm();4546 if (result.success) {47 console.log('Form data:', result.data);48 alert("Form submitted successfully!");49 } else {50 alert("Please fix the errors before submitting.");51 }52 };5354 return (55 <form onSubmit={handleSubmit}>56 <div>57 <label>58 Full Name <span style={{ color: "red" }}>*</span>59 </label>60 <input61 type="text"62 value={state.formData.contact?.name || ""}63 onChange={(e) => methods.updateField('contact', 'name', e.target.value)}64 />65 {state.fieldErrors.contact?.name && (66 <div className="error">67 {state.fieldErrors.contact.name}68 </div>69 )}70 </div>7172 <div>73 <label>74 Email Address <span style={{ color: "red" }}>*</span>75 </label>76 <input77 type="email"78 value={state.formData.contact?.email || ""}79 onChange={(e) => methods.updateField('contact', 'email', e.target.value)}80 />81 {state.fieldErrors.contact?.email && (82 <div className="error">83 {state.fieldErrors.contact.email}84 </div>85 )}86 </div>8788 <button type="submit">Submit</button>89 </form>90 );91}
About this Example
This simple form example demonstrates:
- Basic form configuration and setup
- Field validation with pattern matching
- Form submission and data handling
- Error reporting and validation feedback
- Debug panel to view current form state
The form uses the actual @uplink-protocol/react
and@uplink-protocol/form-controller
packages for form state management and validation. This is the simplest form type - for more complex examples like multi-step forms, dynamic field generation, and advanced validation, see the documentation.