Extjs - Change Textfield Vtype Dynamically
Solution 1:
The other answers are not taking into account that the maskRe needs to change as well. 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.
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
Here's an override that allows you to call setVType on any textfield.
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'];
}
});
Solution 2:
Using Ext.apply worked for me, fiddle here: https://fiddle.sencha.com/#fiddle/4d4
Hope this helps..
Solution 3:
Using Ext.apply and validate() worked for me, fiddle here: https://fiddle.sencha.com/#fiddle/10mp
Use for validation without clear field:
Ext.apply(textField,{vtype:'otherType'});
textField.validate();
Post a Comment for "Extjs - Change Textfield Vtype Dynamically"