This example shows you how to use the data change properties of a coach in a client-side
human service to validate the coach data without exiting the coach.
About this task
The following example demonstrates how to validate the
data that a user enters in the coach so that an error message shows
up in the coach when the data is not valid. The example also demonstrates
how to prevent the user from proceeding to the next step when the
data is not valid.
The example contains a
Prompt for Start and End Dates coach, which has a
Start date field, an
End date
field, and an
OK button. The start date precedes the end
date. To validate the coach data, you will specify a client-side JavaScript in the
coach to perform the validation.
Procedure
- Create the client-side human service that contains the coach to be validated.
See Building a client-side human service.
- In the Variables tab of the human service, create two private variables,
startDate and endDate, and set the
type of each variable to Date.
- In the client-side human service diagram, rename the coach to Prompt for Start and
End Dates.
- In the Coaches tab, from the Variables section of
the palette, drop the startDate and
endDate parameters onto the coach. Leave the default
OK button unchanged.
- In the coach, select the Start date parameter and ensure that it is
bound to the startDate variable in the
Behavior section of the
General tab.
- Repeat step 5 for the End date parameter
to verify that it is correctly bound to the endDate variable,
and then save the coach configuration.
- In the diagram view for the coach, click its Data Change
tab and, in the Script properties, enter the following
JavaScript construct:
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.");
In
the coachValidation object, the first string contains the full
variable path to the elements whose data is to be validated. The second string
is the message for the user, which specifies what is wrong with the data and
tells the user how to fix the problem.
- Optional: To prevent the user from proceeding to the next step when the data is not
valid, complete the following steps:
- In the Variables tab for the human service, add
another private variable named readyToSubmit, and set
its type to Boolean.
- In the Script properties of the selected coach,
append the following JavaScript code to the existing script:
tw.local.readyToSubmit = tw.system.coachValidation.validationErrors.length==0;
- In the Coaches tab, configure the
OK button to be read-only when not ready, as
shown:
- Click Save or
Finish
Editing.
- Click Run
to run the human service.
- In the browser that displays the coach, test the validation
by completing the following steps:
- Set the End date to a date that
precedes the start date. Click OK. The browser highlights the Start date field and displays a
warning icon. If you hover over the warning icon, you see a message that
the start date must precede the end date.
- Set the End date to a date that
succeeds the start date. Click OK. The human service completes successfully because both
dates are valid.