View Full Version : Reject individual records saving

16 Jul 2012, 10:15 AM
When a store is set to autosave, is it possible to process the data object that gets passed to the beforesave event such that certain records within it can be prevented from getting saved to the store, and other allowed to be saved?

It doesn't look like it's designed to work this way - but I was just wondering...

The reason is I need a grid with a bunch of blank/empty rows users can fill in - but I do not want any of these rows to be saved unless data has been entered into them.

Thanks for any input.


16 Jul 2012, 12:19 PM
The save feature of the store will already exclude any records that aren't valid. See http://docs.sencha.com/ext-js/3-4/#!/api/Ext.data.Store-method-save . The source shows the method removing any record that isn't valid. Given that knowledge you just need to provide a mechanism that marks records invalid. This can be done easily by configuring the editors for the cells to not allow blanks. That would then make the records/rows invalid. See http://docs.sencha.com/ext-js/3-4/#!/api/Ext.data.Record-method-isValid for more information.

17 Jul 2012, 6:55 AM
Thanks very much - I considered that actually.
My problem is that a row is invalid only if all cells are blank. Individual cells are allowed to blank...

17 Jul 2012, 8:31 AM
As you already stated, I would simply look at using the store's beforesave event to interrogate the recordset and remove any records that don't pass your own validation?

Alternatively you could run your validation during a grid event (or the input used to edit the cell data), and then cancel the store from saving if the whole row isn't valid?

17 Jul 2012, 9:21 AM
Thanks. Is there support in ExtJS for removing records from the data object passed to beforesave?

What I was going to look into was overriding Ext.data.Record.valid(). Is there a built-in way to do this?


17 Jul 2012, 11:20 AM
This does what I need...

// add blank record to store (and this blank rows to grid)
for(var i = (5 - storeCount); i > 0; i--) {
var record = new store.recordType(GridUtils.initialComboValues(gridId));
// override Ext.data.Record.isValid() to return true
// if there is at least one non-empty cell, and otherwise return false
return this.fields.find(function(f) {
return (!Ext.isEmpty(this.data[f.name])) ? true : false;
},this) ? true : false;
store.insert(storeCount, record);

The result is I add the rows to the store, and they do not get saved until they contain some data.