SMART datagrid v.1 > Examples

Back  Forward

Cell Validation  Example

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. 

1. Edit Validation

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. 

Code -1
    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. 

Commit Level:
Grid - 1
0 rows

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"

Code -2
    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. 

Code -3
    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. 

2. onValidteCell

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. 

Code -4
    grid.onValidateCell = function (grid, index, inserting, value) {
        if (index.dataField() == dsMain.getFieldIndex('weight')) {
            if (value <= 100) {
                DataLudi.throwValidationError(DataLudi.ValidationLevel.ERROR, strings['WeightMsg']);
            }
        }
    };
Grid - 2

View Source JSP 

See Also
Validation Overview
ValidationLevel
EditOptions.commitLevel
GridBase.editOptions
GridBase.onValidateCell
GridBase.onValidateRow
throwValidationError
Examples
Row Validation
Cell Editing
Row Updating
Row Inserting
Row Deleting