In this tutorial, we will explore examples of field validation based on an existing dataset and by sending requests to external services.

Validate input based on the data in a global data set

For example, let's create a form with a single field that will be mandatory for validation. The documentation is here.

Initial client-side validation (in the browser, without sending a request to the server) is activated when the "required" checkbox is checked.

Next, the validation will occur using a custom endpoint.

def body = JSON.fromJSONString(body);
def unit = body.optString('L_WEATHERY_UNIT_ID', null);

if (unit == '' || unit == 'NaN' || unit == null) {
  return Response.ok('ERROR;ユニットIDが入力されていない場合').build();
}

def filter = isEqualFilter('L_UNIT_ID', unit);
def row = readDataset('SYSTEM.WEATHERY_MAPPING', 1, filter, descSort('row_uid'))[0];

if (row == null) {
  return Response.ok('ERROR;ユニットIDが無効の場合').build();
}

return Response.ok(unit as String, 'text/plain').build();

In this code, pay attention to the following points. To receive a JSON format request body, you should use the public type of the endpoint. Otherwise, the validation will be performed incorrectly due to code execution errors. Also, it's essential to check the mandatory input field on the server side.

if (unit == '' || unit == 'NaN' || unit == null) {
  return Response.ok('ERROR;ユニットIDが入力されていない場合').build();
}

Please note the reference to the global dataset during validation.

def row = readDataset('SYSTEM.WEATHERY_MAPPING', 1, filter, descSort('row_uid'))[0];

In case of unsuccessful validation, you should return an error as shown in the example.

if (row == null) {
  return Response.ok('ERROR;ユニットIDが無効の場合').build();
}

For successful validation, you should return a value that will be saved in the dataset.

return Response.ok(unit as String, 'text/plain').build();

Validate input based on the API

For field validation (and/or data retrieval from external services), you should use request configurations. More details are described here.

To validate a field, you should use the previously created configuration for making a request to an external service with the necessary data. We will make the request in two scenarios: during form completion and during data saving. To perform validation during form completion, write the validation formula in a "hidden" input field. To perform validation during data saving, describe a similar functionality in an endpoint. In the case of a successful response, we will receive validation confirmation or a new value for saving. In case of an error, we will return it to the user. Example of a formula for a "hidden" input field:

def fieldId = @L_FACEMA_FIELD_ID;

if (!fieldId) return '';

def credentials = readDataset('FACEMA_CONFIGURATION', 1, null, descSort('ROW_UID'))[0];

if (!credentials) return '';

credentials = credentials.getValues();
def username = credentials['L_USERNAME'];
def password = credentials['L_PASSWORD'];

if (!username || !password) return '';

def authData = new String(Base64.getEncoder().encode((username + ":" + password).bytes));

requestId = httpRequestAsync(256, null, [auth:authData]);

def token;

serviceResponse(requestId, 500, 1001) {
  if (httpStatusCode == 200) {
    token = JSON.fromJSONString(httpBody)['IdToken'].toString();
  }
}

if (!token) return '';

requestId = httpRequestAsync(245, null, [fieldId: fieldId, token:token]);

def kjField = null;

serviceResponse(requestId, 500, 1001) {
  if (httpStatusCode == 200) {
    kjField = JSON.fromJSONString(httpBody)['KjField']?.toString();
  }
}

return kjField ?: '';

The hidden input field can be used to demonstrate the received values in a disabled input field.

  • No labels