0
votes

I have a grid bound to a datasource with 10 fields, all of which are checkboxes. The grid has popup editing enabled.

I want to add a validation that checks (before submit) that atleast one checkbox is checked. How can I add this validation?

All the examples show validation that is per field, whereas I want a validation for the entire row.

UPDATE SaveChanges did not work for me but save did.

save:function(e) {
        if(e.model){
                    var selected = false;
                    for (var key in e.model) {
          if (e.model.hasOwnProperty(key)) {
                        if(typeof e.model[key] == "boolean"){
                          if(e.model[key]){
                            selected = true;                                                                         
                            break;
                          }
                        }
                      }
                    } 
                    if(!selected){                                                                                                       
                      e.preventDefault();
        }
}               
1
Any luck with proposed solution ? - Vojtiik

1 Answers

0
votes

Yes, the validation is not very that flexible. Option 1: Tap into saveChanges event of your grid and iterate through the datasource data items:

 saveChanges: function(e) {
       var data = yourDatasource.data(); // this.dataSource.data()
       for (var i = 0; i < data.length; i++) {
            if (!IsValid(data[i])) {
                e.preventDefault();
                // don't submit
            }
       }

},

Option 2: Attach onclick event on every checkbox you add to the grid and count the clicks. If the clicks you counted are less then row count fail the validation. This sort of validation would also be handled in the "saveChanges" event.