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.

Use 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 completion

  • submitKeepModel β€” keep model after submit (default: true)

  • targetStep β€” step to navigate to after success

  • autoSubmit β€” autosubmit flag (default: false)

  • submitMapping β€” field mapping for submit

  • newData β€” 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 stateMapping

  • endpoint β€” calls API endpoint with mapping

Limitations:

  • Does not support link, modal, or complex action types

  • For 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

  1. Form ID is required β€” API is only available if the FpsForm2 component has a short component ID in data.params.comp_ID.

  2. Timing is critical β€” Always use waitForFormAPI helper or setTimeout to ensure the form is mounted before accessing the API.

  3. Race conditions β€” Use setModel() for updating multiple fields at once, NOT multiple editModel() calls. Form inputs can override changes between sequential updates, causing data loss. Add 100-150ms setTimeout delay if inputs still override your changes.

  4. Model vs ExtendedModel β€” When modifying through the API, both objects are automatically synchronized.

  5. Autosubmit β€” If autosubmit on model change is enabled, programmatic changes via API will also trigger it.

  6. State persistence β€” If state saving to field (saveStateTo) is enabled, programmatic state changes will also be saved on submit.

  7. Refs β€” API uses refs to get current values, so it always returns fresh data.

  8. Cleanup β€” API is automatically removed from window.FpsForm2_API when component unmounts.

  9. Actions vs data-action β€” For simple button actions, consider using the data-action HTML attribute mechanism instead of programmatic callAction. 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?