在EXTJS中将自定义验证作为组件

时间:2016-06-28 05:34:44

标签: extjs extjs6 extjs6-classic

我已经编写了下面的自定义验证

Ext.apply(Ext.form.field.VTypes, {
    valInt: function(v) {
        return /^(\d+(,\d+)*)?$/.test(v);
    },
    valIntText: 'Must be in the form #,#',
    valIntMask: /[\d\,]/i
});

有效。但我想在一个文件中进行所有这样的自定义验证,然后加载它或自动加载它。我该怎么做?

我可以在app.js

中执行以下操作
 Ext.onReady(function() {    
  Ext.apply(Ext.form.field.VTypes, {
        valInt: function(v) {
            return /^(\d+(,\d+)*)?$/.test(v);
        },
        valIntText: 'Must be in the form #,#',
        valIntMask: /[\d\,]/i
    });
});

但是,所有验证后app.js文件都会变大。

2 个答案:

答案 0 :(得分:1)

根据documentation,您可以创建覆盖,例如:

Ext.define('Override.form.field.VTypes', {
    override: 'Ext.form.field.VTypes',

    valInt: function(v) {
        return /^(\d+(,\d+)*)?$/.test(v);
    },

    valIntText: 'Must be in the form #,#',

    valIntMask: /[\d\,]/i
});

app.json中有一个overrides密钥,用于声明覆盖目录,它看起来像:

/**
   * Comma-separated string with the paths of directories or files to search. Any classes
   * declared in these locations will be automatically required and included in the build.
   * If any file defines an Ext JS override (using Ext.define with an "override" property),
   * that override will in fact only be included in the build if the target class specified
   * in the "override" property is also included.
   */
  "overrides": [
    "overrides",
    "${toolkit.name}/overrides"
  ],

答案 1 :(得分:0)

根据@CD。

1)我在我的应用中找到了 overrides 文件夹

2)将此 VTypes.js 文件放在 overrides 文件夹的根目录中:

Ext.override(Ext.form.field.VTypes, {
    digitsOnly: function (value) {
        return this.digitsOnlyRe.test(value);
    },
    digitsOnlyRe: /\\d*$/,
    digitsOnlyText: 'Use digits only!'
});

现在一切正常,全部完成