在ExtJS中,我可以通过vtype传递参数吗?

时间:2011-05-04 05:25:51

标签: extjs

我找不到通过vtype传递参数的方法。
所以我做了一些onfly vtype功能。

var vtypeMaker = function(n){
    Ext.apply(Ext.form.VTypes, {
        myTest : function(v, o){
            console.debug(n); // <-- I can pass my custom value
            // do something
            // ...
            return myRegex.test(v);
        },
        myTestText : 'some message'
    });
}

当我想设置vtype时,我应该首先调用此函数。

vtypeMaker(10);
myTextfield.vtype = 'myTest';

它有效但代码太长。
谁有更好的主意?

感谢。

1 个答案:

答案 0 :(得分:2)

使用验证程序配置传递自定义验证程序功能,并使用 createDelegate 自定义将发送给它的参数:

var myValidator = function( value, custom ) {
    if ( /* value is valid */ ) {
        return true;
    }
    else {
        return 'Field value is not valid';
    }
};

new Ext.form.TextField({
    fieldLabel: 'A text field',
    validator: myValidator.createDelegate(null, [10], true);
});

new Ext.form.TextField({
    fieldLabel: 'Another text field',
    validator: myValidator.createDelegate(null, [14], true);
});

自定义参数在 createDelegate 的第二个参数中设置。将第三个参数设置为 true 将导致将这些自定义参数附加到调用函数的参数(在本例中为字段值)。

相关问题