Engage Demo - Validation
Form
- Create a Form called "Person".
- Add a text field called "FirstName".
- Add a text field called "LastName".
- Add a number field called "Age".
- Add a radio button field called "Gender" and choose valid options.
Globals
- In the Globals area, add
Option Declare
. - Add `Use "VoltScript Testing".
QuerySave
VoltScriptTesting can not only be used for unit / integration testing, it can also be used for validation.
If we suppress the report, we can write unit tests to test the field values. If the tests ran successfully, the document is valid and we can continue saving. If not, we abort saving and return the test failures.
- Declare a variable
doc
as a NotesDocument. - Set it to
Source.Document
. - Declare a string variable called
errors
. - Declare a boolean variable called
validateAge
. - Add error handling to report any error and exit the sub.
- Declare a
testSuite
variable as a new TestSuite, giving it the name "Validate Doc". The name will not be used, but is required. - Add a test to validate that
doc.FirstName(0)
is more than one character.assertTrue
can be used to test the length, usingFullTrim()
andLen()
. - Add a test to validate that
doc.LastName(0)
is not an empty string. - Set the boolean
validateAge
to the result of a test called "Check age is completed", validating theCStr(doc.Age(0))
is not an empty String. - If
validateAge
is true, perform three more tests.- Add a test to validate
doc.Age(0)
is greater than 0. - Add a test to validate
doc.Age(0)
is less than 110. - Add a test to validate that
doc.Age(0)
is a whole number.Fraction()
is a LotusScript function that returns the fractional portion of a number. (For a whole number, the fractional portion should be 0.)
- Add a test to validate
- Add a test to validate
doc.Gender(0)
is not an empty string. - Set
Continue
to the result oftestSuite.ranSuccessfully()
. - If
continue
is False, add code to loop through the results and capture the descriptions for any tests that did not pass.- Add a
ForAll
loop to iterate overtestSuite.results
. - For each test, check if the result is not "Passed". If so, Set
errors
to its existing value, the test description, and a new line. - After the ForAll loop,
MsgBox
the errors to the user.
- Add a
Success
You have successfully added validation for the Form. If you add fields or change fields in the future, you just need to add additional tests. No other changes are needed.
Tip
If you have subforms and want to validate those separately, it may be preferable to move the QuerySave code to a script library. Alternatively, you could set the TestSuite as a global variable.