When creating your form, you may want to implement logic that the Rules Builder just doesn't support, such as setting a field to the value of another field or creating complex logic using 'and' and 'or' in the same condition.
For these types of functions, you will need to use script to build your conditions and text strings. However, not every component has the same structure, and the method for finding the value differs for many field types.
Accessing the component
The first step for each component type is to access the component. There are 2 methods to grab the component: Directly through the data structure or using jQuery.
jQuery is only necessary to get a value if the value is in a repeat and you are already using the jQuery method for iterating through each row of the repeat.
The jQuery method will access the component using this syntax:
$(repeat).getval('Field_Name_txt')
The data structure method will depend on whether you are writing script in the Script Editor or a component's Logic tab. In the Script Editor the syntax would be:
Form.data.Field_Name_txt
In the Logic tab the syntax would be:
data.Field_Name_txt
For the remainder of this article only the Script Editor syntax will be used, however any further additions to the end of the referenced field will remain the same for all three cases.
Text Field, Text Area, Number, Email, (Single) Checkbox...
For simple fields with only one, typed input, the method is just the same as accessing the component as above.
So, for a text field with the field name "Field_Name_txt" you would use:
Form.data.Field_Name_txt
For example, in a function where you are setting the value of one field to the value of one of two fields based on some condition you might write:
var condition = (/*insert some condition here*/) ? true : false;
var value = '';
if (condition) {
value = Form.data.Field_Name1_txt;
} else {
value = Form.data.Field_Name2_txt;
}
Form.data.Output_Field_txt = value;
Note that a single checkbox's value will either be a Boolean true or false.
Checkbox Group, Yes/No Radio, Radio Group
These components have an extra level of data stored within them corresponding to each of the component's options.
These options will each have a Boolean value of either true or false.
To find whether a specific option is checked or selected (often used as a condition in an If-statement) you would use the following syntax:
Form.data.Field_Name_cho["option name"]
There will also be two 'overall' values for the component which you can access. The value and the mergetext value.
You can access the value of the overall component by using:
Form.data.Field_Name_cho.Field_Name_cho
For a Radio Group or Yes/No Radio, this will return the value of the option selected.
For a Checkbox Group, this returns a comma delimited string of all the selected options (e.g. 'option 1,option 3,option 7')
You can access the mergetext value of the overall component by using:
Form.data.Field_Name_cho.Mtext
Again, for Radio Groups and Yes/No Radios, this will return the mergetext value of the option selected, and for a Checkbox Group, this piece of script will return a comma delimited string of all the selected options' mergetext values (e.g. 'special text 1,special text 3,special text 7')
Dropdown List
A dropdown list works slightly differently to the Radio Group and Yes/No Radio, in that you can only get information on the selected value, and not each and every option. You can also directly check the label text for this component.
To get the value use:
Form.data.Field_Name_cho.value
To get the mergetext value use:
Form.data.Field_Name_cho.mtext
To get the label text use:
Form.data.Field_Name_cho.label
Person
Each sub-component in the Person control stores its own value. You can access these values using the following paths (using a component with the field name 'person' as an example):
Title (Salutation): Form.data.person.Sal_cho
First Name: Form.data.person.Name_First_txt
Middle Name: Form.data.person.Name_Middle_txt
Last Name: Form.data.person.Name_Last_txt
Full Name (Read Only): Form.data.person.Name_Full_scr
Address Group
Similarly to the person control, each sub-component of the Address Group stores its own value. You can access these values using the following paths (where our example Address Group has the field name 'tenancy'):
Care of (C/-): Form.data.tenancy.Addr_Co_txt
Building, Floor, Level: Form.data.tenancy.Addr_Level_txt
Street No. & Name: Form.data.tenancy.Addr_1_txt
Suburb / City: Form.data.tenancy.Addr_Suburb_txt
State: Form.data.tenancy.Addr_State_txt
Postcode: Form.data.tenancy.Addr_PC_txt
Country: Form.data.tenancy.Addr_Country_cho
Assembled Full Address (Read Only): Form.data.tenancy.Addr_txt
Repeat Panels
You can directly access the data in a repeat using the following syntax (for a repeat with the field name Repeat_Name_rpt):
var rpt = From.data.Repeat_Name_rpt;
You can then use this 'rpt' variable to access the values for the fields inside each repeat row using a for loop. For example:
var rpt = From.data.Repeat_Name_rpt; //Access the repeat component
var str = ''; //Initialise a variable
if (rpt !== undefined) { //Only proceed if it exists
for (i=0;i<rpt.length;i++) { //Iterate over all rows of the repeat
str += rpt[i].Field_Name_txt; //Get the value of a field in the repeat and
//add it to the str variable (for example)
}
}
Form.data.Output_Field_txt = str; //Make the value of Output_Field_txt a non-
//delimited list of all the values of this
//field in the repeat
In the Logic tab for a field outside of the repeat (i.e. in the main part of the form) a similar piece of script would be used to set the Value as part of the Action section. The only alteration required is the removal of 'Form.' from the first line.
In the Logic tab for a field inside of a repeat where you want to get the value of another field in the same row of the repeat, you would write the script in the Action section as if there were no repeat, and then replace 'data' with 'row'.
If you have any questions about this article, you can submit a request at the top of this page.
Comments
0 comments
Article is closed for comments.