简体   繁体   English

ExtJs-动态更改文本字段VType

[英]ExtJs - Change Textfield VType dynamically

A rather straightforward question. 一个相当简单的问题。 No answer found anywhere. 在任何地方都找不到答案。

Using ExtJs 4.2 使用ExtJs 4.2

I have two custom VTypes, and one textfield. 我有两个自定义VType和一个文本字段。 I want to change that textfield's vtype when the user clicks a radio button. 我想在用户单击单选按钮时更改该文本字段的vtype。

I know that the text field has already been created so doing: 我知道已经创建了文本字段,因此:

textfield.vtype = 'otherType';

won't do it for me. 不会为我做 Neither will: 都不会:

Ext.apply(textField,{vtype:'otherType'});  

So the obvious stuff is out. 所以明显的东西出来了。 Now I'm toying with destroying and re-creating it, but (if it even works) that seems like overkill. 现在,我要破坏并重新创建它,但是(如果它可以工作的话)这似乎有点过头了。

The other answers are not taking into account that the maskRe needs to change as well. 其他答案没有考虑maskRe也需要更改。 Why? 为什么? If you just apply a new vtype the validation will be according to the new vtype, but the characters you're allowed to type does not change along with the new vtype. 如果您仅应用新的vtype,则验证将根据新的vtype进行,但允许输入的字符不会随新的vtype一起更改。

This is the full solution. 这是完整的解决方案。

textfield.vtype = 'otherType';
textfield.validate();
textfield.maskRe = Ext.form.field.VTypes[textfield.vtype + 'Mask'];

If you want to know why, look at the source at how maskRe is set initially: http://docs.sencha.com/extjs/4.2.1/source/Text2.html#Ext-form-field-Text-method-initEvents 如果您想知道为什么,请查看有关最初如何设置maskRe的源代码: http ://docs.sencha.com/extjs/4.2.1/source/Text2.html#Ext-form-field-Text-method- initEvents

Here's an override that allows you to call setVType on any textfield. 这是一个替代,使您可以在任何文本字段上调用setVType。

Ext.define('Ext.overrides.form.field.Text', {
    override: 'Ext.form.field.Text',

    /**
     * Changes the vtype, adjusts the maskRe
     * and revalidates unless withValidate is false.
     */
    setVType: function(newVtype, withValidate) {
        var me = this;

        me.vtype = newVtype;
        if (withValidate !== false) {
            me.validate();
        }
        me.maskRe = Ext.form.field.VTypes[me.vtype + 'Mask'];    
    }
});

Using Ext.apply worked for me, fiddle here: https://fiddle.sencha.com/#fiddle/4d4 使用Ext.apply对我有用,请在这里拨弄: https ://fiddle.sencha.com/#fiddle/4d4

Hope this helps.. 希望这可以帮助..

Using Ext.apply and validate() worked for me, fiddle here: https://fiddle.sencha.com/#fiddle/10mp 使用Ext.apply和validate()对我有用,请在这里拨弄: https ://fiddle.sencha.com/#fiddle/10mp

Use for validation without clear field: 用于没有明确字段的验证:

Ext.apply(textField,{vtype:'otherType'});
textField.validate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM