Logic

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

Form Data:
{
  "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 configuration
2const 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};
29
30// Initialize the form controller
31const { 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 <input
7 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>
17
18 <div>
19 <label>
20 Email Address <span className="text-red-500">*</span>
21 </label>
22 <input
23 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>
33
34 <button type="submit">Submit</button>
35</form>

Form Submission Handling

Implement form submission with validation handling:

1// Handle form submission
2const handleSubmit = (e) => {
3 e.preventDefault();
4 const result = methods.submitForm();
5
6 if (result.success) {
7 console.log('Form data:', result.data);
8 // Process the valid form data
9 // For example: make an API call, show success message, etc.
10 alert("Form submitted successfully!");
11 } else {
12 // Handle validation errors
13 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
Best Practice: Always provide clear validation feedback to users and disable the submit button when the form is invalid to prevent unnecessary submission attempts.

Complete Code Example

Single-Step Form Example
1import React from "react";
2import { useUplink } from " @uplink-protocol/react";
3import { FormController } from "@uplink-protocol/form-controller";
4
5function DynamicFormStepper() {
6 // Define your form configuration
7 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 };
34
35 // Initialize the form controller with useUplink
36 const { state, methods } = useUplink(
37 () => FormController(formConfig),
38 { trackBindings: "all" }
39 );
40
41 // Handle form submission
42 const handleSubmit = (e) => {
43 e.preventDefault();
44 const result = methods.submitForm();
45
46 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 };
53
54 return (
55 <form onSubmit={handleSubmit}>
56 <div>
57 <label>
58 Full Name <span style={{ color: "red" }}>*</span>
59 </label>
60 <input
61 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>
71
72 <div>
73 <label>
74 Email Address <span style={{ color: "red" }}>*</span>
75 </label>
76 <input
77 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>
87
88 <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-controllerpackages 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.