sfRegisterFieldCallback

Binds a function to an event on a form control. It is used to execute a code block when a user performs an action like clicks a button, changes the value in a selectbox etc.

Syntax

sfRegisterFieldCallback(id, event, callback)

Parameters

Name

Type

Mandatory

Description

Name

Type

Mandatory

Description

id

string

yes

Control id of the form control.

event

string

yes

Event (user action on the form control) that should trigger the callback.

See supported events.

callback

function

yes

Code block to be executed when the event occurs.

The callback function contains two parameters, in order:

  • value: the final value of the form field on which the event was triggered

  • index: the index of the form control if the control is present inside a group control

See callback function.

event

An event is a user action on a form control like click of a button, change of text in an input field, change of value in a selectbox etc.

The supported events are:

Name

Description

Applies to

Name

Description

Applies to

change

Occurs when the value of a form field has been changed

Applies to all input controls except Calendar, Form, File, Image, Map and Signature.

click

Occurs when a form control is clicked

Applies to Button.

Usage examples

Example 1

Let us consider a form contains a button SkipAndContinue that allows an operator to save the form without requiring them to complete the rest of the form. To save the form on click of the button,

sfRegisterFieldCallback('control_skipandcontinue', 'click', function(value, index){ sfSaveForm({}); });

Example 2

Let us consider a form contains a numeric control MarginOfError and two other numeric controls MinValidVolumeReading and MaxValidVolumneReading. The standard volume reading is mentioned in another readonly numeric control StandardVolumeReading that has value 10000. When the error margin is set, the minimum and maximum values of the valid volume reading range should be updated accordingly.

sfRegisterFieldCallback('control_marginoferror', 'change', function(value, index){ var stdReading = sfGetValue('control_standardvolumereading'); sfSetValue('control_minvalidvolumereading', stdReading - (stdReading * (value / 100)); sfSetValue('control_maxvalidvolumereading', stdReading + (stdReading * (value / 100)); });