Superform Instance

Gain total programmatic control of your Superform instance.

Superform instances allow you to utilize useful methods and properties, enabling fine-grained control over your forms.

Form Data Methods

getFormData()

With the getFormData() method, you can retrieve form values as an object.

Syntax

const formData = myForm.getFormData() 

Method

Name
Returns
Description

getFormData

Object

Retrieves form values as an object.

Usage example:

window.SuperformAPI = window.SuperformAPI || [];
window.SuperformAPI.push(({ getForm, allForms }) => {
    const myForm = getForm("myForm");
    const formData = myForm.getFormData(); // Returns form data as an object
    console.log(formData); // {email: "name@example.com", ...}
});


Step Lifecycle Methods

beforeStepChange(fn)

With the beforeStepChange method, you can register a listener that will execute right before a step change. This is useful for performing actions like triggering events for analytics.

Syntax

Params Object

The params object passed to the beforeStepChange callback function contains the following properties:

Property
Type
Description

data

Object

The current form data

stepCount

number

The current step index

progress

number

The current progress percentage (0-100)

scores

Object

Contains score data

Usage example:

onStepChange(fn)

With the onStepChange method, you can register a listener that will execute on a step change. This is useful for performing actions like playing/pausing videos, playing animations, or storing user progress via third-party APIs.

Syntax

Params Object

The params object passed to the onStepChange callback function contains the following properties:

Property
Type
Description

data

Object

The current form data

stepCount

number

The current step index

progress

number

The current progress percentage (0-100)

scores

Object

Contains score data

Usage example:


Form Submit Methods

onFormSubmit(fn)

With the onFormSubmit method, you can register a listener that will execute when the form is submitted. This is useful for performing actions like triggering events for analytics or executing your business logic.

Syntax

Params Object

The params object passed to the onFormSubmit callback function contains the following properties:

Property
Type
Description

data

Object

The current form data

stepCount

number

The current step index

progress

number

The current progress percentage (0-100)

scores

Object

Contains score data

Usage example:


Hook Methods

registerNavigationHook(hookName, fn)

Registers a custom navigation hook for form elements, enabling dynamic navigation logic based on form data or other conditions. Read more about navigation hooks here.

Navigation hooks support async returns, allowing you to return a promise that resolves to a string or number.

Parameter
Type
Description

hookName

string

A unique identifier for the hook.

fn

function

A function that executes for navigation. Receives a params object with form data and other details.

Returns

Returns either one valid instruction

step-name

string or Promise<string>

The callback function can return a Step Teleport by step name indicating the next step name.

Linear navigation instruction

"next" "prev" "previous" "back" or Promise<Linear Instruction>

The callback function can return a Linear navigation instruction.

Step Teleport by signed number instruction (i.e +1, -2 or valid signed number)

Signed number as string or Promise<Step teleport string>

The callback function can return a Step Teleport by signed number navigation instruction.

Hook instruction (i.e hook(another_Hook))

hook(HOOK_NAME) or Promise<"hook(HOOK_NAME)">

The callback function can return a hook navigation instruction.

registerInputValidationHook(hookName, fn)

Registers a custom validation hook for form inputs. This method allows you to define complex validation logic programmatically. Read more about validation hooks here.

Parameter
Type
Description

hookName

string

A unique identifier for the hook.

fn

function

A function that executes for validation. Receives a params object with form data and other details.

Returns

true or false

Boolean

The callback function must return a Boolean indicating the validation result (true for pass, false for fail).

Notes

β€’ The hook function must be synchronous; asynchronous operations should be managed outside this method.

β€’ Using a unique hook name is crucial as reusing a hook name will overwrite the previous hook.

Last updated

Was this helpful?