To validate data in a coach in a client-side human service
without accessing a server to do the validation, use client-side validation.
For example, use client-side validation to ensure that required fields
contain data and before dates precede after dates.
About this task
Using client-side validation means that the client-side
human service does not need to do a server call to determine whether
the coach data is valid. If you do need to access a server to validate
the coach data, use server-side validation. For information, see Using server-side validation.
The typical coach
validation pattern consists of a client-side script for coach validation
or a called service, followed by a validation decision that has a
stay on page event that is attached to it. The stay on page event
loops the service flow back to the coach. For an example, see Example: validating data in a coach that is used in a human service.
Procedure
To validate a coach in your client-side human service,
complete the following steps:
- Open the client-side human service that contains the coach
to be validated.
- Add the following elements to the diagram to create a validation
pattern:
- A client-side script to contain the code that performs the validation
- An exclusive gateway to route the flow according to whether the
data is valid
- A stay on page event to return the flow back to the coach when
the coach data is not valid
- Make the following connections:
- Connect the boundary event of the coach to the script.
- Connect the script to the exclusive gateway.
- Connect gateway to the stay on page event. Ensure that this line
is the default flow for the decision. Select the flow line and rename
it to Yes.
- Connect gateway to the rest of the human service flow. Select
the line and rename it to No.
The following screen capture shows the validation pattern. In
the screen capture, the non-default branch connects to an end event.
This branch of the gateway could connect to another coach, to another
gateway, or any other diagram element.Tip: You can have multiple
boundary events with each one leading to a different validation script.
The validation scripts can flow to the same exclusive gateway to reuse
its logic.
- In the diagram, select the script of the validation pattern.
In the Script properties, add JavaScript code that identifies problematic
data. Use the tw.system.coachValidation.addValidationError(String
variableName, String errorMessage) JavaScript API to identify the coach view
that has the problematic data and provide a message that helps the
user correct the problem. For example:
if (tw.local.startDate.getTime() > tw.local.endDate.getTime())
tw.system.coachValidation.addValidationError("tw.local.startDate", "The start date must precede the end date. Set the start date before the end date, and try again.");
Tip: To reuse validation logic, you can define it once then
call that logic multiple times To reuse the validation logic, add
a script element to the diagram to contain the shared logic. Typically,
you place this as the first element in the flow. In the script element,
add a function such as the following example:
tw.local.validationFn = function validateCutomerAndOrder ( ) {
// Shared logic goes here.
}
In the validation scripts, call the function using code
such as:
tw.local.validationFn();
- Select the exclusive gateway and, in its Implementation
properties, create the test. The test is tw.system.coachValidation.validationErrors.length==0.
The test checks for the presence of validation errors and, if there
are none, routes it to the rest of the flow. If there are errors,
the flow goes to the stay on page event so that the coach can show
the control with the problematic data and a message.
- Save your changes, and then click Run .
- In the browser, test that the coach validation works correctly. At run time, the flow must go first to the validation script
to do the coach data validation:
- If the data is valid, the flow moves to the next node.
- If the data in the coach is not valid, the coach validation pattern
loops the flow back to the coach. Error information is passed to the
coach, which indicates the control with the problematic data and can
display the image.