2011-10-06 76 views
1

我正在工作瓦特/分机3.3 如何在Ext.data包级别上进行条件值检查? 如果该人已婚,则需要配偶姓名,否则将其视为空白。ext.data.record有条件验证

我在找的解决方案应该放在数据包上。 (可以说我对数据编辑没有太多控制,但我有责任验证它。) Ext.Data.Field让我说allowBlank,true或false。我想知道是否可以在那里传递一个接受记录的函数,并根据记录上的其他字段返回true或false。

我正在为Ext.data包(Store或Record级别)寻找替代解决方案。

+0

只需在商店(在插入,更新之前)听取适当的'before'事件,执行验证并返回true/false以接受/拒绝修改。 –

回答

2

这取决于您何时进行验证。

如果您要验证时store.load();被调用,那么我建议如下:

myStore.on('beforeload', function(store, loadOptions) { 
     var isValid = true; 
     var modifiedRecs = store.getModifiedRecords(); 
     Ext.each(modifiedRecs, (function(record, index, modifiedArray) { 
      // do validation here 
      // if validation failed, use the following two lines of code: 
      // isValid = false; 
      // return false; // this exits modifiedRecs.each 
     }, this); 
     return isValid; // If falsey, this return statement cancels loading. 
         // Note: the 'loadexception' event will be now be fired 
         // by myStore if isValid is falsey. 
    }); 

如果您要验证每当数据变化在店里,然后使用以下命令:

myStore.on('beforesave', function(store, data) { 
     // simply do validation against `data`. 
     // data will contain an array of records for each type of action that 
     // was being saved, e.g., data['update'] === [updatedRec1, ...]. 
     // if validation failed, just `return false` to cancel saving. 
    }); 

这里的这意味着什么是falsey