sfSetDefaultValue
Sets the provided value to a form control (only if the field is empty).
This function is a variation of sfSetValue and is intended to set a default value to form controls. If the field already has a value, no action is done. If the field is empty, then the provided default value is set to the field.
Syntax
sfSetDefaultValue (id, value, index, skipChangeEventTrigger)
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
id | string | yes | Control id of the form control. |
value | any | yes | Default value to be set to the form control. It must be a value that is supported by the form control. See values supported by form controls. |
index | number | no | Index of the form control. Applicable only if the control is present inside a group control. The index is a zero-based index. |
skipChangeEventTrigger | boolean | no | Indicates if the change event trigger should be skipped. When a new value is set to a form control or the existing value in the control is changed, a change event is triggered by default that evaluates all runtime expressions that are either bound to or depend on the control. This parameter can be used to prevent the change event so that the runtime expressions are not re-evaluated. It is generally not recommended to set this parameter to true as it may lead to inconsistency in data. However, it may be useful to prevent issues that may arise due to cyclical dependencies in runtime expressions. |
Applies to
Control type | Applicable | Remarks/Exceptions |
---|---|---|
Yes | Applies to all input controls except Bar/QR code scan, Calendar, Form, Identifier, Image and Signature. | |
No |
| |
No |
| |
Yes | Applies to HTML only. |
Value accepted by the function differs with the form control. See values supported by form controls.
Usage examples
Example 1
Let us assume a form contains a text field TankNumber. To set a default value to the text field,
sfSetDefaultValue('control_tanknumber', 'TK010')
Example 2
Let us assume a form contains a group control QuestionsChecklist with a numeric field CapacityInLitres. To set a default value to the numeric field in each row,
for(var i = 0; i < sfField('div_questionschecklist').recordCount(true); i++) {
sfSetDefaultValue('control_capacityinlitres', '100', i)
}
Example 3
Let us assume a form contains a selectbox control Terminal that displays the list of terminals from a dataset Terminals. The dataset has properties TerminalName and TerminalContactEmail, in addition to Code that is the default property available to all dataset items.
The selectbox displays TerminalName in the dropdown. To select a terminal as default in the dropdown, the Code of the intended terminal should be set as its value. So,
sfSetDefaultValue('control_terminal', 'BPMEX')
As a result, the terminal with Code BPMEX is selected in the selectbox.
Example 4
Let us assume a form contains a file control Reference. A user handbook PDF has been uploaded as a resource named UserHandbook. To bind the user handbook by default to the file control,
As a result, the PDF uploaded as a resource is shown as a file in the field Reference.
Supported value formats
The function accepts value in different formats depending on the form control.
Form control | Format | Remarks |
---|---|---|
Set 0: 0 or 1 | Accepts one of the select options (depending upon the set used) | |
YYYY-MM-DD | Accepts a valid date | |
YYYY-MM-DD HH:MM | Accepts a valid date and time (in 24-hour format) | |
| Accept a valid HTML text | |
Resource name | Accepts the name of a resource. The resource must be a supported file type and must present in the domain Multiple files from resources can be assigned by providing a comma separated list of resource names. | |
| Accepts a valid coordinates (as comma separated latitude and longitude in decimal degrees) | |
| Accepts the code of the dataset item to be selected Multiple items can set by providing a comma separate list of codes. | |
| Accepts a valid numeric value | |
| Accepts a valid numeric value | |
| Accepts the code of the dataset item to be selected | |
| Accepts the code of the dataset item to be selected | |
| Accepts a valid numeric value | |
| Accepts a text | |
| Accepts a text | |
HH:MM | Accepts a valid time (in 24-hour format) | |
0 or 1 | Accepts the state of the toggle (0 for off and 1 for on) |