zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    angularjs制作的iframe后台管理页切换页面
    javascript读取本地文件
    nginx Engine X静态网页服务器介绍
    关于 bounds 和 frame
    iOS 开发常见函数
    HTTP POST GET 本质区别详解(转)
    从 UIAlertView 到 UIAlertController
    学习 AFNetworking 3.0
    UICollectionView详解
    UITableView整理
  • 原文地址:https://www.cnblogs.com/mlzs/p/4212139.html
Copyright © 2011-2022 走看看