5

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.

jwl
  • 10,268
  • 14
  • 53
  • 91
  • Are you using any other frameworks that you could delegate this portion of "work" to? As you have observed, the default validation of Slickgrid is limited only to the `validate` function of an `editor` which is passed only the value as a parameter. You could modify the source to achieve the desired behavior, but personally I would delegate this type of validation to another framework (ie [Knockout Validation](https://github.com/Knockout-Contrib/Knockout-Validation)) ... though I am partial to this combination as I have experience with it. – Origineil Sep 29 '14 at 14:40
  • No, I would prefer to stick with built in SlickGrid validation. – jwl Sep 29 '14 at 14:44
  • I've [started a demo](http://jsfiddle.net/origineil/nLpzjL32/) of full grid validation. The `validate` button will run any column defined `validator` against each data entry. *Default validation error behavior* is to not allow the closure of an editor input field, so I've added cell flashing to illustrate where you would need to implement the desired failure handling. Secondly, I don't know what a *default* value would be, so I've used only numbers with a requirement that they be `> 0`. – Origineil Oct 03 '14 at 16:01
  • @Origineil - wow, that's cool. Post this as an answer and I'll give you the bounty (even if it's not exact match, I'd hate for the points to go to waste, and I appreciate the effort) – jwl Oct 03 '14 at 19:21

1 Answers1

2

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

Origineil
  • 3,108
  • 2
  • 12
  • 17