SMART datagrid v.1 > Examples

Back  Forward

Row Validation  Example

It will run one or more validations specified in editOptions.validations when the row editing is committed. And, after running the validation, GridBase.onValidateRow event will be fired, and you will be able to run another validation within this event handler. 

After committing the row editing and before running the validation on a row basis above, it will run validations of each column. Please refer to CellValidation example. 

Code -1
    grdMain.editOptions().setValidations([{
        "expression": "values['year'] >= 2000 && values['year] < 2020",
        "level": DataLudi.ValidationLevel.WARNING,
        "message": strings["YearMsg"],
        "mode": DataLudi.ValidationMode.UPDATE
    }]);

In the code above, it has set expression to occur an error when Year field value is out of 2000 to 2020. Please refer to Expression Overview about the expression structure. And, please refer to Validation Overview help about the variable list which can be used in the expression of EditValidtion for the row validation. 

"mode" specifies the validation time by ValidationMode constant. In the code, it only runs during Row Updating. 

level can be specified by ValidationLevel constant such as "error", "warning", "info", "ignore". 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 changing the value of Year and commtLevel settings below. 

Commit Level:
Grid - 1
0 rows

If the validation is a little bit complex and SMART datagrid expression is not correct, you can specify JavaScript callback function through EditValidation.callback property as below code. If explicitly return false within the callback function, it will be proceeded in the same way as EditValidation. Please refer to Validation Overview help about scope composition of callback parameter. 

Code -2
    grdMain.editOptions().setValidations([{
        "callback": function (scope) {
            var v = scope.row.getValue('year');
	        if (v < 2000 || v >= 2020) return false;
        },
        "level": DataLudi.ValidationLevel.ERROR,
        "message": strings["YearMsg"],
        "mode": DataLudi.ValidationMode.UPDATE
    }]);

Instead of setting Edit Validations in EditOptions.validations property, you can also build onValidateRow event handler of the grid to run the row validation. 

onValidateRow event will be fired once before committing the row editing. If throw an exception by calling throwValidationError within this event handler, the result will be the same as the validation using Validation object. 

Code -3
    grdMain.onValidateRow = function (grid, row, inserting, values) {
        if (!values.comm_code) {
            DataLudi.throwValidationError(DataLudi.ValidationLevel.ERROR, strings['CommCodeMsg']);
        }
        if (inserting) {
            if (values.weight < 100) {
                DataLudi.throwValidationError(DataLudi.ValidationLevel.INFO, strings['WeightMsg']);
            }
        } else {
            if (values.weight < 200) {
                DataLudi.throwValidationError(DataLudi.ValidationLevel.INFO, strings['WeightMsg2']);
            }
        }
    };

inserrting parameter will be false during Row Updating and true during Row Appending. values is json object which contains the currently entered values in the editing row as its properties. If the validation error occurs, you must call throwValidationError and transfer the error level and message. 

View Source JSP 

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