pfudala

Use web service to set rules and custom validations

Blog Post created by pfudala Champion on Jan 12, 2016

Products : Nintex Forms 2013, Nintex Forms 2010

 

Most of the forms use information stored within the site containing the form. And sometimes, they need to match external data (business databases).

Here is a sample that could help you to achieve this. I used a Northwind OData service so that you can directly test the solution on your environment.

 

Into the "Custom JavaScript" section in the form settings, insert these 2 functions :

//Defines if input is a Northwind territory
function IsNorthwindTerritory(input)
{
  var isValid = false;
  var dfd = new $.Deferred();
  NWF$.ajax({
  url: "http://services.odata.org/V4/Northwind/Northwind.svc/Territories?$filter=TerritoryDescription eq '" + input + "'",
  async: false
  }).always(function (data)
  {
  isValid = (data.value.length > 0);
  });
  return isValid;
}


//Uses previous function to perform a custom validation on a control
function IsNorthwindTerritoryValidation(source, arguments)
{
  var isKnownTerritory = IsNorthwindTerritory(arguments.Value);
  if(!isKnownTerritory)
  {
  //errorMessage could be set here but you should better use Nintex Forms Settings
  //source.errormessage = "This is not a Northwind territory".
  }
  arguments.IsValid = isKnownTerritory;
}

 

The first one defines the business rule. It could be directly use in a Nintex Forms rule.

The second one uses the first one and set objects attended in a Custom Validation.

 

First example : Use the business rule to show/hide a panel

Choose a panel you would like to hide if it does not correspond to the business rule.

Add a rule from the ribbon button and use the previous "IsNorthwindTerritory" function in the condition formula.

You should see the second panel only if you type a Northwind territory (ie. Bedford).

 

Second example : Use the business rule as a custom field validator

Select the field "Territory" and in its settings, set a custom validation, based on the second JavaScript function "IsNorthwindTerritoryValidation".

You should obtain a such behavior when performing a form validation :

 

Now you just have to replace business logic by yours to be able to perform validations with external data.

Outcomes