Rules and Scripting

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

show, required, disabled or hideInput

In the browser, whenever form data changes

Default value

Data tab → Custom Default Value → JavaScript Default

value

Once, when the form opens, if the component is empty

Calculated value

Data tab → Calculated Value → JavaScript Value

value

Whenever form data changes. Optionally again on the server at submission (Calculate on server).

Custom validation

Validation tab → Custom Validation

valid

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 data.userId

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

data

The value of every component on the form, by property name, for example data.firstName

All scripts

row

Inside a Data Grid or Edit Grid, the values of the current row

Conditions, calculated values, validation

value

Set this to the component's new value

Default and calculated values

input

The value entered in the component being validated

Custom validation

moment

The Moment.js date library

Calculated values and conditions (not default values)

user

The signed-in user's record, for example user.data.email. Empty for anonymous users.

Calculated values

submission

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 an else.

  • 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

  1. Open the form in a separate tab and fill it in as a user would, including leaving fields empty.

  2. 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.

  3. 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 or Hide Components

Show components based on other values, with or without code

Making Components Required or Disabled

Make components required, disabled or masked conditionally

Custom Field Validation

Check values with your own rules and messages

Combining Values

Join values, such as a full name from its parts

Using Buttons for Custom Actions

Run a script when the user selects a button

Working With Data Grids

Total columns, number rows and fill grids

Working With Dates

Fill in today's date, record signing dates, and calculate dates

Pre-filling Fields From the Form Link

Fill fields from values in the form's link

Getting Data From a URL in a Script

Load data from a web address

Changing a Form by GlobalCapture Validation Step

Show different components at each workflow step

Common Script Snippets

Short scripts for users, text, numbers and choices