Validating client-side coaches using server-side validation

You can validate coaches in client-side human services using server-side validation.

About this task

In server-side validation for coaches in client-side human services, the human service flow calls a service on the server to do the validation. The client-side human service then handles any messages that are returned by the server.

Use server-side validation to validate data in a coach in a client-side human service when the following situations apply:
  • The validation must access private or confidential data that should not be made available to the client. Server-side validation is the more secure approach.
  • The validation must access server-based data or large amounts of data to do the validation. For example, you have Order coach in which users can order parts and you want to validate that a part is in stock. It is not practical to use client-side validation in this situation. Loading large amounts of data impacts the performance of the client.
  • You are migrating heritage human services to client-side human services and you want to reuse some or all of the validation scripts you had in the heritage human service. The client-side human services call the validation service that you previously used. However, examine the script logic to move appropriate validation code to the client to minimize server calls.
If you do not need to access a server to validate the coach data, you can use client-side validation. For information, see Using client-side validation.
The following procedure uses a help desk client-side human service as an example. The human service consists of several fields and a Create Ticket button.
Tip: Where possible, combine your server-side validation with server-side logic that does work as shown in the examples in this procedure. That is, a single server-side service validates the ticket and also saves it. While you could do this in separate steps in the human service, combining them in this way provides better performance. It also places related logic together to produce more maintainable models.

Procedure

To validate a coach by using server-side validation:

  1. Open the Process Designer desktop editor.
  2. Create the service that validates the coach data. The service cannot be a human service but it can be any other type. The example code uses a general system service called Save Ticket Service.
    1. In the Variables page, create an input variable that contains the data from the coach
    2. Create the validationResults(CoachValidation) private variable to contain any validation messages.
    3. Create the hasValidationErrors(Boolean) private variable to indicate when there is data that fails the validation tests.
    4. In Diagram page, add the following elements:
      • A server script to validate the data.
      • A decision gateway to route the flow according to whether the data is valid
      • A nested service or other elements in the service flow to handle when the data is valid. For example, such as a nested service that saves the coach data or an end event to complete the service.
      • An error end event
    5. Connect the Start node to the script and connect the script to the decision. Connect the gateway to the flow that handles valid data such as a service that saves coach data. Select the flow line and rename it to Yes. Connect the gateway to the error end event. Select the flow line and rename it to No. The resulting diagram looks something like this example:
      Service diagram
    6. Add JavaScript code to the script to validate the coach data. The script code must create a CoachValidation object that contains an addCoachValidationError instance for every problem with the coach data. If there is a problem with the coach data, the script must also set isValid to false. For example, if the help ticket must contain information in the Summary field, add code like the following example:
      // Create the CoachValidation object
      tw.local.validationResults = new tw.object.CoachValidation();
      tw.local.hasValidationErrors = false; 	// Set to false unless the data fails a validation test
      if (tw.local.ticket != null) {
      	// If there is nothing in the summary, add the error to the Coach Validation object
      	if (tw.local.ticket.summary == null || tw.local.ticket.summary.length == 0) {
          	tw.local.hasValidationErrors = true;
      		tw.system.addCoachValidationError(tw.local.validationResults, 
      			"tw.local.ticket.summary", "The problem summary is required.");
      	}
      }
    7. In the Implementation properties of the decision, add the following code to the No branch:
      tw.local.hasValidationErrors == true
      The No branch of the decision with tw.local.hasValidationErrors in the first field, == in the second field, and true in the third field.
    8. In the Implementation properties of the error end event, set Error code to coachValidation and set Error mapping to the tw.local.validationResults variable. These properties are the mechanism by which the client-side human service receives the validation messages.
      Error end event properties
  3. Open your client-side human service. The Process Designer web editor displays the human service.
  4. In your client-side human service, add a service to your diagram and wire your coach to the service.
    The coach that is connected to a service
  5. In the Implementation properties of the client-side serves, select to call a service and then select the server-side validation service that you created in step 2.
    The implementation properties of the client-side service that shows it calling the server-side service that does the data validation
  6. In the Data Mapping properties, map the variable that contains the coach data to the appropriate business object.
  7. Add an intermediate event to the service. Because it is attached to a service, the intermediate event is an error boundary event.
  8. In Implementation properties of the error boundary event, set it to catch specific errors and map the error data to the tw.system.coachValidation variable.
  9. Connect the error boundary event to a stay on page node.
    The boundary intermediate event on the nested service connected to the stay on page event.