In this lesson, you create a validation service to ensure that the Create Requisition Coach contains valid data when the user clicks Next.

Before you begin

This lesson builds on the artifacts that are created in the and modules.

About this task

The create position request coach contains fields in which users might enter invalid data or where users must make a choice. The planned starting date field is an example of where users might enter invalid data because user must not enter past dates. The position type field is an example of where users must make a choice because the service flow depends on whether the user selects a new position or an existing position. You create a validation script to check the data in the coach. The script validates the data in several controls and prevents the flow from leaving the coach until the data in these controls is valid.

Procedure

Add validation to the create position request coach:

  1. Make sure that the Create Position Request CSHS human service is open in the web Process Designer.
  2. Select the Diagram tab.
  3. From the pallette, add the following objects from left to right below the create position request coach.
    • Client-side script. Relabel the script to Validation script.
    • Exclusive gateway. Relabel the gateway decision to Validation errors?
    • Intermediate event. The intermediate event automatically becomes a stay on page event.

    The validation script and validation errors gateway decision are added below the create position request coach. The intermediate event has changed to a stay on page event and is beside the validation errors decision.

  4. Make the following changes to the flow:
    • Delete the Next flow that leaves the create position request coach.
    • Connect the create position request coach to the validation script. This connection now has the Next label. When the user clicks Next in the coach, the flow goes to the script.
    • Connect the validation script to the validation errors decision.
    • Connect the validation errors decision to the stay on page event.
    • Connect the validation errors decision to the position type decision.

    Move the nodes around so that you can clearly see the flow lines and the labels.

    The flow now goes from the create position request coach to the script and then to the gateway decision. The flow then either goes to the stay on page event or to the position type decision.

  5. Edit the Script properties of the Validation script and add the following JavaScript code to perform the validation:
    tw.local.coachValidation = {}; 
    tw.local.coachValidation.validationErrors = [];
    
    function checkString(value) {
      return value != null && value == "";
    }
    
    function checkDate(value) {
       var date = new Date();
       return value == null || ( date.getTime() > value.getTime() );
    }
    
    function checkList(value) {
      return value != null && value.length == 0;
    }
    
    
    if ( checkString(tw.local.currentPosition.positionType) ){
      tw.system.coachValidation.addValidationError("tw.local.currentPosition.positionType", "You must select a position type.");
    }
    
    if ( checkString(tw.local.requisition.location) ){
      tw.system.coachValidation.addValidationError("tw.local.requisition.location", "You must select a location.");
    }
    
    
    if ( checkDate(tw.local.requisition.date) ){
      tw.system.coachValidation.addValidationError("tw.local.requisition.date", "You must select a date that is in the future.");
    }
    
    if ( checkString(tw.local.requisition.empType) ){
      tw.system.coachValidation.addValidationError( "tw.local.requisition.empType", "You must select an employment type.");
    }
    
    if ( checkString(tw.local.requisition.department) ){
      tw.system.coachValidation.addValidationError("tw.local.requisition.department", "You must select a department.");
    }
  6. Define the branches for the validation errors gateway decision:
    1. Select the line that flows from decision to the Stay on page event. In the line properties, type its
      name as Yes.
    2. Select the line that flows from decision to the Position type decision. In the line properties, type its name as No.
    3. Select the validation errors decision and open its Implementation properties.
    4. For the No branch, add the following statement: tw.system.coachValidation.validationErrors.length == 0
      The first field of the No branch contains the entire decisionThe tw.system.coachValidation.validationErrors variable is specifically for storing the results of a validation check.
  7. Save your changes.

Lesson checkpoint

You created the validation used to keep the flow on the page until the user provides values for the required fields.

In this lesson, you learned

  • How to create a validation script
  • How to connect a coach to the validation script
  • How to handle the flow for a coach with valid data and how to
    handle a coach that does not have valid data

In the next lesson, you conduct Playback Three.