Rules add logic to a form: showing a component only when it's needed, calculating totals, checking that values make sense, or filling in data automatically. Many rules need no code. For the rest, GlobalForms lets you write short JavaScript scripts on a component.
Basic rules need no programming. Scripts need some knowledge of JavaScript. The examples in this section are starting points you can copy and adapt.
Where rules go
Rules are set on the component they affect. Each type of rule has its own place in the component's settings and its own variable to set.
|
Rule |
Where |
The script sets |
Runs |
|---|---|---|---|
|
Show or hide, require, disable, or mask input |
Conditional tab. Choose the property, then Simple (no code) or Advanced |
|
In the browser, whenever form data changes |
|
Default value |
Data tab → Custom Default Value → JavaScript Default |
|
Once, when the form opens, if the component is empty |
|
Calculated value |
Data tab → Calculated Value → JavaScript Value |
|
Whenever form data changes. Optionally again on the server at submission (Calculate on server). |
|
Custom validation |
Validation tab → Custom Validation |
|
As the user enters data, and on the server at submission |
|
Button action |
Button, Display tab, Action: Custom → Button Custom Logic |
Component values directly, such as |
When the button is selected |
|
Before a wizard is submitted |
Form settings. See Wizard Forms. |
(stops submission by throwing an error) |
When a wizard form is submitted |
Components that contain scripts show an icon in the form builder, so you can find them quickly. See Form Builder Tools.
Variables available to scripts
|
Variable |
Contains |
Available in |
|---|---|---|
|
|
The value of every component on the form, by property name, for example |
All scripts |
|
|
Inside a Data Grid or Edit Grid, the values of the current row |
Conditions, calculated values, validation |
|
|
Set this to the component's new value |
Default and calculated values |
|
|
The value entered in the component being validated |
Custom validation |
|
|
The Moment.js date library |
Calculated values and conditions (not default values) |
|
|
The signed-in user's record, for example |
Calculated values |
|
|
The whole submission, including its metadata |
Calculated values |
Property names
Scripts refer to components by property name, not label. Find it on the component's API tab. Property names are case-sensitive and must be unique on the form. When you plan to write scripts, give components short, clear property names, such as startDate rather than textField3.
Write reliable scripts
Most script problems come from values that haven't been filled in yet, or that are text when a number was expected. A total of data.amount1 + data.amount2 shows NaN while either field is empty, and can join two text values ("10" + "5" gives "105") instead of adding them.
-
Check that values exist before using them:
if (data.startDate) { … } -
Convert to numbers before doing arithmetic, and treat empty as zero:
(Number(data.amount1) || 0) + (Number(data.amount2) || 0) -
Always set the result variable, including when values are missing, for example
value = '';in anelse. -
Keep scripts on the component they affect. Avoid changing other components from inside a calculated value or condition, except where an example shows it.
Test and troubleshoot
-
Open the form in a separate tab and fill it in as a user would, including leaving fields empty.
-
Open the browser's developer tools (F12) and select the Console tab. Script errors appear there with the component's property name, for example An error occured within custom function for totalAmount.
-
To see values while testing, add
console.log(data);to a script. Remove it when you're done.
Security
-
Scripts and conditions run in the user's browser, and users can change what happens there. Use them for convenience, not to enforce rules. Checks that run again on the server when the form is submitted are: components that are always Required, the other built-in checks on the Validation tab, Custom Validation, and calculated values with Calculate on server. Conditional Required is checked only in the browser.
-
Scripts are saved in the form definition, which anyone who can open the form can read. Never put passwords, API keys or other secrets in a script.
Examples
|
Page |
Shows how to |
|---|---|
|
Show components based on other values, with or without code |
|
|
Make components required, disabled or masked conditionally |
|
|
Check values with your own rules and messages |
|
|
Join values, such as a full name from its parts |
|
|
Run a script when the user selects a button |
|
|
Total columns, number rows and fill grids |
|
|
Fill in today's date, record signing dates, and calculate dates |
|
|
Fill fields from values in the form's link |
|
|
Load data from a web address |
|
|
Show different components at each workflow step |
|
|
Short scripts for users, text, numbers and choices |