sencha touch Model validations 自定义验证 二选一输入验证、重复验证、时间验证、比较验证、条件验证(2015-1-14)
2015-01-08 23:28
411 查看
项目初始化时执行以下代码
模型验证用法:
不知道模型验证怎么用的可以看看我以前的文章
/article/5153273.html
//重写模型,方便进行自定义验证 Ext.define("Ext.zh.data.Model", { override: "Ext.data.Model", validate: function () { var errors = Ext.create('Ext.data.Errors'), validations = this.getValidations().items, validators = Ext.data.Validations, length, validation, field, valid, type, i; if (validations) { length = validations.length; for (i = 0; i < length; i++) { validation = validations[i]; field = validation.field || validation.name; type = validation.type; //这里重写了代码,验证时将模型做参数加入 //方便进行验证 valid = validators[type](validation, this.get(field), this); if (!valid) { errors.add(Ext.create('Ext.data.Error', { field: field, message: validation.message || validators.getMessage(type) })); } } } return errors; } }); //自定义验证,这里就多了个formData就可以获取到所有数据,能方便验证 Ext.apply(Ext.data.validations, { chooseOneMessage: '验证字段与指定字段之中只能有一个字段有值', //验证字段与指定字段之中只能有一个字段有值 //这个值可以验证非空或者通过正则表达式验证 //forComparison 指定字段 //matcher 正则表达式 chooseOne: function (config, value, formData) { var name = config.forComparison, otherValue = formData.data[name], matcher; if (value && otherValue) { return false; } value = value || otherValue; matcher = config.matcher; if (matcher) { return !!(matcher && matcher.test(value)); } return true; }, //验证字段与指定字段的值必须一致 //可用于修改密码时验证重复密码 //forComparison 指定字段 repeat: function (config, value, formData) { var otherValue = formData.data[config.forComparison]; if (value != otherValue) { return false; } return true; }, repeatOneMessage: '两次输入不一致', //时间验证,只能验证时间 //验证字段的值不能大于当前时间 //dateFormat 时间格式化格式,这样时间值就不必是标准格式 //dateFormat(例子)'Y-m-d H:i:s', 值为 2014-01-01 21:23:21就可以直接验证了 timeCheck: function (config, value) { var dateFormat = config.dateFormat; if (dateFormat) { value = Ext.Date.parse(value, dateFormat, true); } if (value > new Date()) { return false; } return true; }, timeCheckMessage: '验证字段的时间不能大于当前时间', //可以验证数字时间等支持比较的数据类型 //验证字段的值不能大于与指定字段的值 //dateFormat 时间格式化格式,这样时间值就不必是标准格式 //dateFormat (例子)'Y-m-d H:i:s', 值为 2014-01-01 21:23:21就可以直接验证时间了 compareTime: function (config, value, formData) { var otherValue = formData.data[config.forComparison], dateFormat = config.dateFormat; if (dateFormat) { value = Ext.Date.parse(value, dateFormat, true); otherValue = Ext.Date.parse(otherValue, dateFormat, true); } if (value > otherValue) { return false; } return true; }, compareTimeMessage: '验证字段的值不能大于与指定字段的值', //指定字段值为指定值时验证字段才进行验证,否则不验证 //这个值可以验证非空或者通过正则表达式验证 //forComparison 指定字段 //matcher 正则表达式 //factor 指定字段条件值 forOne: function (config, value, formData) { var forValue = formData.data[config.forComparison], factor = config.factor, matcher; if (factor != forValue) { return true; } if (!value) { return false; } matcher = config.matcher; if (matcher) { return !!(matcher && matcher.test(value)); } return true; }, forOneMessage: '某个其他字段值为指定值时才进行验证' });
模型验证用法:
field: 'name', //二选一验证 type: 'chooseOne', //其他字段 forComparison: 'name1', //同时支持正则表达式 //重复验证用法类同 matcher: /(^[0-9]+(.[0-9]{2})?$)/, message: '请输入name或name1!'
不知道模型验证怎么用的可以看看我以前的文章
/article/5153273.html
相关文章推荐
- sencha touch Model validations(模型验证,自定义验证)
- sencha touch自定义表单验证
- MVC自定义数据验证(两个时间的比较)
- ExtJS中表单验证使用自定义vtype(两次输入密码重复)示例
- [转]MVC自定义数据验证(两个时间的比较)
- sencha touch自定义表单验证
- Struts2 Day 05 : 运行流程、自定义拦截器、上传下载、表单重复提交、输入验证
- js限制只能输入日期或时间的输入框,并加验证
- ASP.Net MVC探索之路 - Model的比较验证
- 验证查询条件 - 起始时间和终止日期
- Entity SQL 时间条件比较
- outlook 2007 提示您重复输入密码在特定的网络条件下
- js日期时间格式验证,时间比较
- js正则表达式验证文本框输入SQL条件是否合理
- 使用AT SELECTION-SCREEN事件验证用户输入条件的反思
- Entity SQL 时间条件比较
- js使用正则验证输入的时间格式
- 一个输入框输入日期或时间,并验证输入是否有效
- Using Validations and Associations in Sencha Touch
- js日期时间格式验证,时间比较