MultiForm Custom API
Control your form like a boss β programmatic API to read/write data, manage steps, submit forms, and trigger actions from your JavaScript.
Multistep Form provides a public API for programmatic form control from external JavaScript code. This allows you to integrate the form with custom logic and dynamically modify form data and state.
API Access
The API is available through the global object window.FpsForm2_API[formId], where formId is the short component ID from form settings (data.params.comp_ID).
// Get form API by ID
const formAPI = window.FpsForm2_API['myFormId'];Mounting and Timing Issues
Important: The form API is registered during component mount. If your script runs before the form is mounted, the API will be undefined.
Recommended Solution: waitForFormAPI Helper
waitForFormAPI HelperUse this helper function to wait for the API to become available:
function waitForFormAPI(formId, callback, timeout = 5000) {
const start = Date.now();
const check = () => {
if (window.FpsForm2_API && window.FpsForm2_API[formId]) {
callback(window.FpsForm2_API[formId]);
} else if (Date.now() - start < timeout) {
setTimeout(check, 100);
} else {
console.error('Form API not available after ' + timeout + 'ms. Check form ID.');
}
};
check();
}
// Usage
waitForFormAPI('myFormId', (formAPI) => {
console.log('β
API ready!');
// Your code here
formAPI.editModel('firstName', 'John');
});Alternative: Use setTimeout with 500-1000ms delay if you know the form will mount quickly.
API Methods
Getting Data
getModel()
Returns the current form model (flat object with fields).
getExtendedModel()
Returns the extended form model (with nested objects for links/arrayLinks).
getState()
Returns the current form state (step, popup, custom fields).
getOriginalModel()
Returns the original model (before user changes).
Modifying Model
editModel(field, value)
Changes a single field in the model. Supports nested paths with dot notation.
Important: Automatically updates both model and extendedModel.
setModel(newModelData)
Merges the provided object with the current model (merge, not replace).
replaceModel(newModel)
Completely replaces the model (replace, not merge).
Modifying State
editState(field, value)
Changes a single field in state. Supports nested paths.
setState(newStateData)
Merges the provided object with the current state.
replaceState(newState)
Completely replaces the state (use with caution, may break form logic).
Programmatic Submit
submit(options)
Programmatically triggers form submission.
Options:
finishβ callback after completionsubmitKeepModelβ keep model after submit (default:true)targetStepβ step to navigate to after successautoSubmitβ autosubmit flag (default:false)submitMappingβ field mapping for submitnewDataβ additional data{ model: {...}, state: {...} }resetModelβ reset model after submit
Calling Actions
callAction(actionIdOrName, callback)
Programmatically triggers a form action by ID or name.
Supported action types:
stateβ updates state/model via stateMappingendpointβ calls API endpoint with mapping
Limitations:
Does not support
link,modal, or complex action typesFor those, use direct API methods (
editState,submit, etc.)
Note: For simpler cases, consider using the data-action mechanism in HTML elements. It's easier to implement and doesn't require JavaScript. See the data-action documentation for details.
Utilities
refreshOptions()
Refreshes dynamic options in form fields (for select/autocomplete with endpoints).
Usage Examples
1. Validation and Data Modification
2. Step Management
3. Syncing with External Data
4. Custom Triggers and Calculations
5. Programmatically Opening Popups
6. Calling Actions Programmatically
Important Notes
Form ID is required β API is only available if the FpsForm2 component has a short component ID in
data.params.comp_ID.Timing is critical β Always use
waitForFormAPIhelper orsetTimeoutto ensure the form is mounted before accessing the API.Race conditions β Use
setModel()for updating multiple fields at once, NOT multipleeditModel()calls. Form inputs can override changes between sequential updates, causing data loss. Add 100-150mssetTimeoutdelay if inputs still override your changes.Model vs ExtendedModel β When modifying through the API, both objects are automatically synchronized.
Autosubmit β If autosubmit on model change is enabled, programmatic changes via API will also trigger it.
State persistence β If state saving to field (saveStateTo) is enabled, programmatic state changes will also be saved on submit.
Refs β API uses refs to get current values, so it always returns fresh data.
Cleanup β API is automatically removed from
window.FpsForm2_APIwhen component unmounts.Actions vs data-action β For simple button actions, consider using the
data-actionHTML attribute mechanism instead of programmaticcallAction. It's simpler and requires no JavaScript code.
Complete Working Example
AI Prompt Template
Use this prompt template with any AI assistant (ChatGPT, Claude, etc.) to generate custom code for your form:
Prompt Usage Examples:
Example 1: Calculate total
Example 2: Conditional field population
Example 3: Custom validation
Example 4: External data integration
Example 5: Action trigger
Pro tip: Just copy the prompt template, replace [DESCRIBE YOUR TASK HERE] with your specific requirement, and paste it into any AI chat. You'll get ready-to-use code! π
Last updated
Was this helpful?