extjs vtype: check value using another value in the same row
I have开发者_C百科 an Ext.grid.Panel
and i want to validate user input: there is a field named delete
and a field named string
. Valid input for delete
is a number, which can't be greater than the length of a string
field in the same row. I already know how to use vtype
so now i have
delete : function(val, field){
var expr = new RegExp("^[-]?[0-9]*[\.]?[0-9]*$");
var num = expr.test(val);
if (!num) return false; //can't be not a number
else{
//have no idea...
}
}
I have no idea how to access string
value for the same row.
Hope it's quite clear. Thanks!
It's actually much simpler and cleaner if you define your editor as a variable, you can just refer to it in the fields validator method and get the active record, for example:
// create editor as a variable
var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit: 1,
autoCancel: false
});
// referring to the edited record in the validator function
{
xtype: 'datecolumn',
header: 'Comp Date',
dataIndex: 'comp_date',
width: 100,
format: 'j-M-Y',
editor: {
xtype: 'datefield',
format: 'j-M-Y',
validator: function(value) {
var record = cellEditing.getActiveRecord(); // get active record
if (Ext.Date.parse(value, 'j-M-Y') < record.get('start_date')) {
return 'Cannot complete before start date';
} else return true;
}
}
}
You should handle validateedit( Ext.grid.plugin.Editing editor, Object e, Object eOpts )
event.
The second argument, e
, contains reference to the record that is edited (e.record
). You could use this record to get both fields(string
and delete
) and perform validation accordingly.
Assigning false
to e.cancel
will cancel the editing.
well, there's a workaround, but it's ugly:
rowEditing.on({
scope : this,
afteredit : function(roweditor, changes, record, rowIndex) {
var can_save = true;
var records = store.getRange();
for(var i = 0; i < records.length; i++) {
if(records[i].data['delete'] >= records[i].data['string '].toString().length) {
Ext.MessageBox.alert('Input error', "blahblah");
}
I accessed the plugin the following way:
this.ownerCt.ownerCmp.plugins
That gives you access to the cellEditing plugin and from there you could do a:
var record = plugin.getActiveRecord();
精彩评论