New to H2O 4.0 is the ability to add logic to your forms in the form of scripting.
Examples of use would be to add the values entered into multiple controls and display the result in another control or validating a
checkbox control to prevent the form from being saved if it's not checked. The examples used here are available for download
in the Downloads page (
www.DNNForms.com) in the form H2O Code.
There are six H2O Script statement types. If, SetVal, SetVis, Save, Redirect and AddRole.
To use this code, you must add the H2O Logic control to your form and type the code into the H2O Script field of the control.
There is no limit on the number of H2O Logic controls you can use.
||The SetVal statement will set a control's value to the value you specify.
An example is displayed to the left and you can click here for a
full screen shot. There are two ways to use this function:
- SetVal AddNumbers = [Value1 Value2]
- SetVal AddNumbers = Value
In these examples, AddNumbers is a Text Box control on the form that is being set to the result.
In the H2O Code sample form, Value1 and Value2 are also Text Box controls on the form.
The result: The data typed into control Value1 and control Value2 are added and then stored in the AddNumbers control.
The SetVal statement evaluates string and numeric data types. If you enter 35 into the Value1 control then H2O
will handle this data as a numeric. For example, if you enter 50 into Value1 and 100 into the Value2 control the
result will be 150. If you enter DotNet into the Value1 control and Nuke into the Value2 control, the result will
When coding H2O Script, you can use the following operators in your expressions:
||Not Equal To
||Not Greater Than
||Not Less Than
||Not Greater Than
The If statement is a little more complex. An example of this is visible in the image below.
In the NameCompare control of the H2O Code form, the script is set to concatenate the values entered into the FirstName and LastName
controls and then place the result into the lblDisplayname control. An extra step is taken to ignore this operation if a value
was not entered into either the LastName or the FirstName control.
The structure of the If statement is: If (expressions) SetVal expressions
If (LastName != Null,FirstName != Null) SetVal lblDisplayName = [FirstName LastName]
There are two sections to this statement, the evaluation and then the result. The evaluation (If (LastName != Null,FirstName
!= Null)) basically asks if LastName contains a value and FirstName contains a value then proceed to the result
The result (SetVal lblDisplayName = [FirstName LastName])
concatenates the values entered into the FirstName and LastName controls and then places the result into the lblDisplayName control on
You are unlimited in the number of expressions you can add to be evaluated in the If statement.
Each expression must be separated by a comma. If you only have one control to evaluate, then there is no need to enter a comma.
The SetVal keyword in the If statement follows the same rules as stated above for the SetVal statement.
The SetVis statement sets the visibility of a control. This statement should be used on controls that used to collect
data. If a control is not visible, then it is not rendered to the page and H2O can not retrieve its value for data entry since
it doesn't exist.
The SetVis statement is used in the H2OTerms control of the H2O Code form. The statement in this control is:
SetVis chkAcceptTerms = False lblAcceptTerms
This statement checks if the checkbox chkAcceptTerms in the form is checked, if it is not checked then the label control
lblAcceptTerms visibility is set to true making it visible on the screen.
The Save statement evaluates your expression and controls whether or not the form is saved.
If the expression evaluates to True, then the form is Saved. If the expression evaluates to False, then the form is not saved
. An example of this is in the H2OPreventSave control of the H2O Code sample form.
The script: Save chkAcceptTerms = True
This statement checks if the checkbox control chkAcceptTerms is checked, if it is checked the form is saved. Otherwise, the
form is not saved.
The statements AddRole and Redirect follow the same pattern as the SetVis statement. Examples of these
- AddRole control = True 90 (In this example, 90 is the Role ID to add the user to)
- Redirect control = True
(In this example, if control = true then the user is redirected to OnyakTech.com)
Once you master the basics, you can take it farther by using more than one H2O Logic control to add
powerful processing to your forms. For example, with multiple H2O Logic controls (as there is in the H2O Code sample form) you
can easily create logic that makes sure the user has accepted your terms, if they have not, then you want to display a message and
prevent the form from being saved. You may also want to add the number of times they fail at checking the checkbox, store the
value in a hidden field and then redirect them to another page if that value exceeds 10.