- How to pre-fill forms with initial data
- How to track form changes in real time
- How to access form data imperatively with viewer references
- How to update form data from your application
- How to work with form data in actions
- Best practices and common pitfalls
Understanding Form Data Flow
Before diving into specific APIs, let’s understand how form data flows in FormEngine:- Initialization: When
FormViewermounts, it populates form fields with values from theinitialDataprop - User Interaction: As users interact with the form, data changes trigger validation and events
- Data Access: You can access current data through multiple channels
- Data Updates: You can update form data from outside the form
Basic Concepts: initialData, data, and errors
The initialData Prop
The initialData prop allows you to pre-fill form fields when the form loads. This is useful for edit forms, form wizards, or any scenario where you need to load existing data.Live example
live
Current Form Data (data)
The current form data represents the values of all form fields at any given moment. You can access it through several methods that we’ll explore in this guide.Validation Errors (errors)
When form validation fails, error messages are stored in theerrors object. Each key in the errors object corresponds to a field key, and
the value is the error message.
Subscribing to Form Changes with onFormDataChange
The simplest way to track form data changes is by subscribing to the onFormDataChange callback. This function receives the updated form data and any validation errors each time a field changes.Basic Subscription Example
Live example
live
Advanced: Debouncing Form Changes
For performance optimization, you might want to debounce form change events:Imperative Data Access with viewerRef
Sometimes you need to access form data imperatively - for example, when a button is clicked or at specific points in your application flow. This is where viewerRef comes in handy.Setting Up a Viewer Reference
Live example
live
Getting Validation Results
ThegetValidationResult function performs silent
validation - it validates the form data without displaying errors on the form itself. This is useful when you want to check validation
programmatically before taking action like form submission.
Validation results provide more detailed information than just error messages:
Live example
live
Updating Form Data
When you update theinitialData prop, FormViewer automatically updates the form fields. This is the recommended approach for most use
cases.
Live example
live
Working with Form Data in Actions
Actions provide a powerful way to manipulate form data from within the form itself. You can access and modify form data directly in action handlers.Accessing Data in Code Actions
Live example
live
Code Actions with Data Access
You can also use inline JavaScript code actions to work with form data:logFormData Action example
Practical Examples and Patterns
Example 1: Form with Save Draft Functionality
live
Example 2: Multi-Step Form with Data Persistence
live
Common Pitfalls and Solutions
Pitfall 1: Missing Field Keys
Problem: Data doesn’t bind to fields because keys don’t match. Solution: Ensure field keys ininitialData match the key (or dataKey) property in your form JSON.
Pitfall 2: Data Type Mismatch
Problem: Form expects a string but receives a number. Solution: Convert data to the expected type:Summary
FormEngine Core provides a comprehensive set of tools for form data management:- initialData – For pre-filling and reactive updates
- onFormDataChange – For real-time data tracking
- viewerRef – For imperative data access and manipulation
- actions – For data manipulation within form logic
For more information:
- Validation guide
- Actions and Events guide
- FormEngine Core API reference
Next steps
- Form Submission — Handle form submission and process user input
- Validation — Validate form data and display error messages