Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating all cells in all rows of SlickGrid

Given a SlickGrid, how can I trigger all cells in all rows to be validated? Or perhaps to use JavaScript to trigger once cell to be validated (which I can then use against all cells in all rows)?

The use case is one where the user must edit every cell and provide something other than the default, and we want to make sure they have done so and we want to show the default validation error behavior if they have not.

Currently it appears that validation only happens on the fields that are edited.

like image 724
jwl Avatar asked Oct 31 '22 16:10

jwl


1 Answers

As observed, the default validation of Slickgrid is limited to the validate function of an editor which checks for any available validator passing along only the value as a parameter. In order to supply additional contextual information, a custom editor is required, or more specifically a custom validation function.

this.validate = function() {
   if (args.column.validator) {
       args.newValue = $input.val()
       var validationResults = args.column.validator(args);

       if (!validationResults.valid) {
          return validationResults;
       }
   }

  return { valid: true, msg: null };
};

Each column would then need a validator within which the default value would be checked against either a new value coming from the editor or the existing value along with any other required validation aspects.

var Validator = function(args) {

  //validate the existing value or the incoming editor value
  var value = args.newValue ? args.newValue : args.item[args.column.field]
  var result = value > 0 
  return {valid: result}
}

To validate the entire grid provide a validation method that iterates over each row looking at each column for a validator. Based on the validation results, a relational mapping of rowIndex -> collection of failures is built up to be passed to the native onValidationError event. This allows for a subscription to handle user notification of the presence of errors. In addition, the validation results can be used to style failures by providing specific metadata to the grid

var validateColumns = function(args){

 var failures=[];

 for (c in columns) {
   var column = columns[c]
   if (column.validator) {
      if(!column.validator({row: args.row, item: args.item, column: column}).valid){
         failures.push({columnIndex: c, column: column, rowIndex: args.row, item: args.item})
      }
   }
 }
 return failures;
}

grid.validate = function() {
   var rowFailures = {}
   for (r in data) {
     //ignore our metadata provider (if applicable)
     if(r == 'getItemMetadata'){continue;}

     var failures = validateColumns({item: data[r], row: r})
     if(failures.length > 0){
       rowFailures[r] = failures;
     }
    }

    if(Object.keys(rowFailures).length > 0){
     grid.onValidationError.notify({"rowFailures": rowFailures}, new Slick.EventData())
    }
   }

Fiddle

like image 69
Origineil Avatar answered Nov 10 '22 15:11

Origineil