model 的验证

时间:2024-01-19 20:13:32
 Ext.onReady(function(){
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{ name: 'name', type: 'string' },
{ name: 'gender', type: 'string'},
{ name: 'username', type: 'string' },
{ name: 'f', type: 'string'},
], validators:{
//判断id不得大于1位,
//age: { type: 'presence'},
name: { type: 'length', min: 2, max: 5},
gender: { type: 'inclusion', list: ['Male', 'Female'] },
username: { type: 'exclusion', list: ['Admin', 'Operator'] },
f: function(value){ alert(value); }, //自定义方法判断
}
}); var instance = Ext.create('User', {
name: '1',
gender: 'Male',
username: 'not Admin',
f: 'hello',
}); var validation = instance.getValidation();
//验证成功返回false 验证失败返回true
alert(validation.isValid()); //判断验证是否正确
console.log(validation);
});