2011-10-06 81 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