When commit the cell editing and at the point to commit the row editing, it will run Validation specified by each column. And, GridBase.onValidateCell event will also provide another validation opportunity.
You can specify one or more validations in DataColumn.validations property, and if even one of them has not passed, an exception will be thrown when commit the change. And, an error icon will be displayed in the data cell before committing. The validation is specified by each Column rather than Data Field.
In the example below, it has set "A value must be contained" in "Product code" column. Please refer to Expression Overview topic about the expression structure. Please refer to Validation Overview topic about the variable list which can be used in the expression of Column Edit Validation.
column.setValidations([{
"expression": "Value should not be empty.",
"level": DataLudi.ValidationLevel.WARNING,
"message": strings["CommCodeMsg"]
}]);
level can be specified by ValidationLevel constant such as "error", "warning", "info", "ignore", and the corresponding data cell will be set by this error level if the validation fails. After this, when commit editing the row, if it is more critical than the error level set in commitLevel of grid editOptions, the editing will not be committed.
You can test by emptying the value of Product Code and changing commtLevel settings below.
You can also enable to run the validation only during Row Appending or Updating. In the example, two EditValidation.mode of "Year" column have been set as "insert" and "update". If do not specify, it will always run "always".
column.setValidations([{
"expression": "value >= 2000",
"level": DataLudi.ValidationLevel.INFO,
"message": "Year must be at least 2000.",
"mode": DataLudi.ValidationMode.INSERT
}, {
"expression": "value < 22000",
"level": DataLudi.ValidationLevel.INFO,
"message": "Year must be smaller than 2020.",
"mode": DataLudi.ValidationMode.UPDATE
}]);
Instead of expression which uses SMART datagrid expression structure, you can also run the validation by specifying callback function in callback property which uses JavaScript. Please refer to Validation Overview topic about scope parameter which is transferred to callback function.
column.setValidations([{
"callback": function (scope) {
if (scope.value < 1000) {
return false;
}
},
"level": DataLudi.ValidationLevel.ERROR,
"message": "Weight must be at least 1000."
}]);
If explicitly return false within the callback function, the validation will fail.
Instead of setting Edit Validations in DataColumn.validations property, you can also build onValidateCell event handler of the grid to run the cell validation.
During the row editing and before committing the cell editing, onValidateCell event will be fired, and if throw an exception by calling throwValidationError within this event handler, the result will be the same as the validation using Validation object. In the example below, if the value of "Weight" field is below 100, it has set to fail the validation with ERROR level.
grid.onValidateCell = function (grid, index, inserting, value) {
if (index.dataField() == dsMain.getFieldIndex('weight')) {
if (value <= 100) {
DataLudi.throwValidationError(DataLudi.ValidationLevel.ERROR, strings['WeightMsg']);
}
}
};