Validation Checks in Tables

This sample runs validation checks for values inside tables, when saving the record.
In this particular sample, the radio buttons determine the validation for the number fields.

Sample Image

errorcheck.gif

Sample Code

// 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 (!table.hasOwnProperty(i)) { continue; }
            var row = table[i].value;
            var num = row[NUMBER].value;
            var numType = row[NUMBERTYPE].value;
            var isEvenNumber = num % 2 === 0 ? true : false;

            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;
    });
}());

Notes

Make sure to match the Field code of your fields, with the Field code written in the code.
Note that tables themselves also have Field codes.

 

Refer to the Tutorial page, on how to add scripts to your app.

Was this article helpful?
0 out of 0 found this helpful
Comments
Please sign in to leave a comment.