sencha touch Model validations 自定义验证 二选一输入验证、重复验证、时间验证、比较验证、条件验证(2015-1-14)...
生活随笔
收集整理的這篇文章主要介紹了
sencha touch Model validations 自定义验证 二选一输入验证、重复验证、时间验证、比较验证、条件验证(2015-1-14)...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目初始化時執行以下代碼
1 //重寫模型,方便進行自定義驗證 2 Ext.define("Ext.zh.data.Model", { 3 override: "Ext.data.Model", 4 validate: function () { 5 var errors = Ext.create('Ext.data.Errors'), 6 validations = this.getValidations().items, 7 validators = Ext.data.Validations, 8 length, 9 validation, 10 field, 11 valid, 12 type, 13 i; 14 if (validations) { 15 length = validations.length; 16 for (i = 0; i < length; i++) { 17 validation = validations[i]; 18 field = validation.field || validation.name; 19 type = validation.type; 20 //這里重寫了代碼,驗證時將模型做參數加入 21 //方便進行驗證 22 valid = validators[type](validation, this.get(field), this); 23 if (!valid) { 24 errors.add(Ext.create('Ext.data.Error', { 25 field: field, 26 message: validation.message || validators.getMessage(type) 27 })); 28 } 29 } 30 } 31 return errors; 32 } 33 }); 34 //自定義驗證,這里就多了個formData就可以獲取到所有數據,能方便驗證 35 Ext.apply(Ext.data.validations, { 36 chooseOneMessage: '驗證字段與指定字段之中只能有一個字段有值', 37 //驗證字段與指定字段之中只能有一個字段有值 38 //這個值可以驗證非空或者通過正則表達式驗證 39 //forComparison 指定字段 40 //matcher 正則表達式 41 chooseOne: function (config, value, formData) { 42 var name = config.forComparison, 43 otherValue = formData.data[name], 44 matcher; 45 if (value && otherValue) { 46 return false; 47 } 48 value = value || otherValue; 49 matcher = config.matcher; 50 if (matcher) { 51 return !!(matcher && matcher.test(value)); 52 } 53 return true; 54 }, 55 //驗證字段與指定字段的值必須一致 56 //可用于修改密碼時驗證重復密碼 57 //forComparison 指定字段 58 repeat: function (config, value, formData) { 59 var otherValue = formData.data[config.forComparison]; 60 if (value != otherValue) { 61 return false; 62 } 63 return true; 64 }, 65 repeatOneMessage: '兩次輸入不一致', 66 //時間驗證,只能驗證時間 67 //驗證字段的值不能大于當前時間 68 //dateFormat 時間格式化格式,這樣時間值就不必是標準格式 69 //dateFormat(例子)'Y-m-d H:i:s', 值為 2014-01-01 21:23:21就可以直接驗證了 70 timeCheck: function (config, value) { 71 var dateFormat = config.dateFormat; 72 if (dateFormat) { 73 value = Ext.Date.parse(value, dateFormat, true); 74 } 75 if (value > new Date()) { 76 return false; 77 } 78 return true; 79 }, 80 timeCheckMessage: '驗證字段的時間不能大于當前時間', 81 //可以驗證數字時間等支持比較的數據類型 82 //驗證字段的值不能大于與指定字段的值 83 //dateFormat 時間格式化格式,這樣時間值就不必是標準格式 84 //dateFormat (例子)'Y-m-d H:i:s', 值為 2014-01-01 21:23:21就可以直接驗證時間了 85 compareTime: function (config, value, formData) { 86 var otherValue = formData.data[config.forComparison], 87 dateFormat = config.dateFormat; 88 if (dateFormat) { 89 value = Ext.Date.parse(value, dateFormat, true); 90 otherValue = Ext.Date.parse(otherValue, dateFormat, true); 91 } 92 if (value > otherValue) { 93 return false; 94 } 95 return true; 96 }, 97 compareTimeMessage: '驗證字段的值不能大于與指定字段的值', 98 //指定字段值為指定值時驗證字段才進行驗證,否則不驗證 99 //這個值可以驗證非空或者通過正則表達式驗證 100 //forComparison 指定字段 101 //matcher 正則表達式 102 //factor 指定字段條件值 103 forOne: function (config, value, formData) { 104 var forValue = formData.data[config.forComparison], 105 factor = config.factor, 106 matcher; 107 if (factor != forValue) { 108 return true; 109 } 110 if (!value) { 111 return false; 112 } 113 matcher = config.matcher; 114 if (matcher) { 115 return !!(matcher && matcher.test(value)); 116 } 117 return true; 118 }, 119 forOneMessage: '某個其他字段值為指定值時才進行驗證' 120 });?
模型驗證用法:
1 field: 'name', 2 //二選一驗證 3 type: 'chooseOne', 4 //其他字段 5 forComparison: 'name1', 6 //同時支持正則表達式 7 //重復驗證用法類同 8 matcher: /(^[0-9]+(.[0-9]{2})?$)/, 9 message: '請輸入name或name1!'不知道模型驗證怎么用的可以看看我以前的文章
http://www.cnblogs.com/mlzs/p/3341175.html
總結
以上是生活随笔為你收集整理的sencha touch Model validations 自定义验证 二选一输入验证、重复验证、时间验证、比较验证、条件验证(2015-1-14)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智能电视:跳出那个坑
- 下一篇: C#的Equals不区分大小写