cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Using a Validator with a Service to perform more advanced analytics

No ratings

Validator widgets provide an easy way to evaluate simple expressions and allow users to see different outcomes in a Mashup.

Using a validator is fairly intuitive for simple expressions, such as is my field blank? But if we need to evaluate a more complex scenario based on multiple parameters, then we can user our validator with a backing service that will perform more complex analytics.

To show how services can work in conjunction with a validator widget, let’s consider a slightly more complicated scenario such as: A web form needs to validate that the zip or postal code entered by the user is consistent with the country the user selected.


Let’s go through the steps of validating our form:

  • Create a service with two input parameters. Our service will use regex to validate a postal code based on the user’s country.  Here’s some sample code we could use on an example Thing:

//Input parameters: Country and PostalCode (strings)

//Country-based regular expressions:

var reCAD = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/;

var reUS = /^\d{5}(-\d{4})?$/;

var reUK = /^[A-Za-z]{1,2}[\d]{1,2}([A-Za-z])?\s?[\d][A-Za-z]{2}$/;

var search = "";

//Validate based on Country:

if (Country==="CAD")

               search = reCAD.exec(PostalCode);

else if (Country==="US")

               search = reUS.exec(PostalCode);

else if (Country==="UK")

               search = reUK.exec(PostalCode);

(search == null) ? result = false: result = true;

  • Set up a simple mashup to collect the parameters and pass them into our service

  • Add a validator widget
  • Configure the validator widget to parse the output of the service. ServiceInvokeComplete on the service should trigger the evaluation and pass the result of the service into a new parameter on the widget called ServiceResult (Boolean). The expression for the evaluator would then be: ServiceResult? true:false

  • Based on the output of the validator, provide a message confirming the postal code is valid or invalid
  • Add a button to activate the service and the validator evaluation

Of course, in addition to providing a message we can also use the results of the validator to activate additional services (such as writing the results of the form to the database).

For an example you can import into your ThingWorx environment, please see the attached .zip file which contains a sample mashup and a test thing with a validator service.

Version history
Last update:
‎Feb 21, 2017 02:25 PM
Updated by:
Labels (1)
Attachments
Tags (1)