model 的验证

 1 Ext.onReady(function(){

 2              Ext.define('User', {  

 3                 extend: 'Ext.data.Model',  

 4                 fields: [  

 5                     { name: 'name', type: 'string' },  

 6                     { name: 'gender', type: 'string'},

 7                     { name: 'username', type: 'string' },

 8                     { name: 'f', type: 'string'}, 

 9                 ],  

10               

11                 validators:{  

12                     //判断id不得大于1位,

13                     //age: { type: 'presence'},

14                     name: { type: 'length', min: 2, max: 5},

15                     gender: { type: 'inclusion', list: ['Male', 'Female'] }, 

16                     username: { type: 'exclusion', list: ['Admin', 'Operator'] },

17                     f: function(value){ alert(value);  },  //自定义方法判断

18                 }

19             });  

20 

21             var instance = Ext.create('User', { 

22                 name: '1', 

23                 gender: 'Male',

24                 username: 'not Admin',

25                 f: 'hello',

26             });   

27               

28             var validation = instance.getValidation();  

29             //验证成功返回false 验证失败返回true

30             alert(validation.isValid());  //判断验证是否正确

31             console.log(validation);

32         });

 

你可能感兴趣的:(Model)