Validations for Table Field Values

Contents

Overview

This article introduces how to run validation checks for fields within tables of an App.

Sample Image

In this example, the table includes Number fields and Radio button fields. Validation checks run when users try to save the record. If the values in the Radio button options do not match the description of the value in the Number field, an error is displayed. If a number is not placed in the Number field, an error is also displayed.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Validation check for odd and even numbers in tables

(function() {
  'use strict';

  var TABLE = 'checking_table'; // field code of the table of where the error checks will occur
  var NUMBER = 'mynumber'; // field code of a number field in the table
  var NUMBERTYPE = 'radio_button'; // field code of a radio button field in the table
  var events = ['app.record.create.submit', 'app.record.edit.submit'];

  kintone.events.on(events, function(event) {
    var record = event.record;
    var table = record[TABLE].value;

    for (var i in table) {
      if (Object.prototype.hasOwnProperty.call(table[i])) {
        continue;
      }
      var row = table[i].value;
      var num = row[NUMBER].value;
      var numType = row[NUMBERTYPE].value;
      var isEvenNumber = num % 2 === 0;

      if ((isEvenNumber && numType === 'Odd number') || (!isEvenNumber && numType === 'Even number')) {
        row[NUMBERTYPE].error = 'Type does not match';
      } else if (!num) {
        row[NUMBER].error = 'Insert a number';
      }
    }
    return event;
  });
}());

Reference