Overview
FastField supports skip logic and embedded rules that can perform the following actions:
- Hide and show fields
- Hide and show form sections
- Set fields as required or not required
- Enable or disable fields
These actions are possible through conditional logic. For example, you can set up a rule for a field that says, "When this field equals Yes, hide the next question." This helps to create a user-friendly and streamlined form that adapts to your users' needs.
Field rules can be applied without any JavaScript coding knowledge using our Field Rules menu, which lets you input various conditions through dropdown menus. If you would like to manually enter JavaScript rules, you can do so by creating a Rule Script.
How to Add Field Rules (Without JavaScript)
To apply rules to a field without manually entering a script:
- Select the field you'd like to apply logic to in the Form Builder.
- Select the Rules icon next to the field name or in the settings menu on the right. This will open the Field Rulesmenu.
- Select the Rules Scrip tab.
- Use the dropdown menus to input the conditions of your rule for that field.
*Note: To create a rule that can span multiple fields and fire multiple actions, use our Advanced Rules feature. Advanced rules are form-level rules that can trigger actions based on conditions that occur across multiple fields throughout the form. Field Rules, however, only trigger actions from a single field.
How to Write a Field Rules Script (With JavaScript)
To write a rule script:
- Select the field you'd like to apply logic to in the Form Builder.
- Select the Rules icon next to the field name or in the settings menu on the right. This will open the Field Rulesmenu.
- Select the Rule Script tab.
- Enter your rule script in the text box.
You can also use a Calculation Field to create rules with Javascript. To learn more, click here.
Referencing Field Values in Rules
To access a field value in a rule condition, wrap the field key with the $ character.
- For example, for a field with a field key of first_name, you would use the following syntax to check if the first name is John:
if ($first_name$ == 'John')
Built-in Functions
FastField supports built-in functions in addition to the standard functions that you will find in the JavaScript library. The following functions can be used to interact with the form model:
- hide(optional fieldKey) - hide a field. If the Field Key is not specified, the field that the script is written for will be used.
- show(optional fieldKey) - show a field. If the Field Key is not specified, the field that the script is written for will be used.
- hideSection(optional sectionKey) - hides an entire section. If the Section Key is not provided, the current section will be used.
- showSection(optional sectionKey) - shows an entire section. If the Section Key is not provided, the current section will be used.
- required (optional fieldKey) - sets a field as required. If the Field Key is not specified, the field that the script is written for will be used.
- notRequired(optional fieldKey) - sets a field as not required. If the Field Key is not specified, the field that the script is written for will be used.
- disable(optional fieldKey) - sets a field as disabled. If the Field Key is not specified, the field that the script is written for will be used.
- enable(optional fieldKey) - sets a field as enabled. If the Field Key is not specified, the field that the script is written for will be used.
- float(value, emptyVal) - safely converts a value to a float
- string(value) - converts value to a string
- integer(value, emptyVal) - safely converts a value to an integer
- bool(value) - safely converts a value to a boolean (true/false)
- listContainsValues($list_field_key$, ['value 1', 'value 2']) - checks if List from a List Selector field contains a set of values
- listContainsValue($list_field_key$, ['value']) - checks if List from a List Selector field contains a single value
- sumSubform($subform$, 'field key') - sums all the values of a Field Key within all instances of a particular subform
- hideOnReport('fieldKey') - conditionally hide a field from the default reports. If the Field Key is not specified, the field that the script is written for will be used.
- showOnReport('fieldKey') - conditionally show a field on the default reports. If the Field Key is not specified, the field that the script is written for will be used.
Examples
Hiding/Showing Fields
To hide a field through the script, the 'hide' built-in function can be used.
The following example illustrates how to hide the field if the 'married' switch field is set to true (Yes):
if ($married$ == true)
show();
else
hide();
Specifying Field Keys to Hide and/or Show Fields
The following example illustrates how to hide two fields with the same rule.
if ($married$ == true)
{
show('spouse_name');
show('spouse_age');
}
else
{
hide('spouse_name');
hide('spouse_age');
}
Hiding/Showing Form Sections
To hide a section within a form through the script, the 'hideSection' built-in function can be used.
The following example illustrates how to hide the section if the field with the field key of 'first_name' is equal to Sam.
if ($first_name$ == 'Sam')
hideSection();
else
showSection();
In the example above, All fields in the active section will be hidden when the first name is equal to Sam, and conversely, all fields will be visible if the first name is not equal to Sam.
Specifying Field Keys to Hide and/or Show Fields
To hide a specific section, you can pass the section key as a parameter to the hideSection function.
The following example illustrates how to show/hide two sections with the same rule.
if ($have_children$ == true) {
showSection('children_data');
showSection('marriage_data');
}
else {
hideSection('children_data');
hideSection('marriage_data');
}
Checking for Values in a List
To check if a List Selector contains values, you can use the list contains values function. The following example illustrates how to write these rules:
if (listContainsValues($favorite_color$,['red']) && $age$ > 25) {
show();
}
else {
hide();
}
In the example above, the field is shown if the favorite color selected is red and the age is greater than 25.
Performing Calculations in Rules
You may also perform calculations such as:
int total = 0;
if ($married$ == true)
{
total = $required_deposit$ * 2.0;
}
else
{
total = $required_deposit$;
}
if (total > 50)
{
required();
}
else
{
notRequired();
}
In the example above, the total is increased by 2 times the required deposit if the individual is married.
Safe Number Conversions
To convert a string (text) value to a numeric value, you can use the following built-in functions to avoid null conversion errors:
- float(value, emptyVal)
- integer(value, emptyVal)
-
if (float($height$) > 2.5)
This method will return 0 if the height is not a number. To provide your own default, you can supply the second optional parameter emptyVal. In the following example, -99 would be returned if the height is not a number:
if (float($height$, -99) == -99)
Summarizing List Picker Scores
List Picker fields allow for a score to be set with each list item. To use the score value in a rule or a Computed Label field calculation, use the same syntax when referencing a field's value (e.g., wrap field key in $) but add a pound (#) symbol in front of the reference string. For example:
(#$listpicker_3$ + #$listpicker_11$) * 12