您的位置:首页 > 其它

sencha touch Model validations 自定义验证 二选一输入验证、重复验证、时间验证、比较验证、条件验证(2015-1-14)

2015-01-08 23:28 411 查看
项目初始化时执行以下代码

//重写模型,方便进行自定义验证
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: