PDA

View Full Version : [Beta] Ext.ux.plugins.DisableValidator



alexodus
3 Jun 2008, 8:01 AM
Inspired by RemoteValidator, I realized this plugin:

Ext.namespace('Ext.ux', 'Ext.ux.plugins');

/**
* Disable Validator
* To be used by form fields like TextField, NubmerField, TextArea, ...
*/
Ext.ux.plugins.DisableValidator = {
init:function(field) {
// save original functions
var myfield = field;
var isValid = field.isValid;
var validate = field.validate;
var disableValidate = field.disableValidate;
// apply remote validation to field
Ext.apply(field, {
remoteValid:true
// private
,isValid:function(preventMark) {
return isValid.call(this, preventMark);
}

// private
,validate:function() {
if (myfield.disableValidate==true) {

if (myfield.getValue().length == 0) {
this.markInvalid(Ext.form.TextField.prototype.blankText);
return false;
}
this.clearInvalid();
return true;
}
else {
var clientValid = validate.call(this);
if(!this.disabled && !clientValid) {
return false;
}
if(this.disabled || clientValid ) {
this.clearInvalid();
return true;
}
return false;
}
}

,updateValidate:function() {
// alert('updateValidate='+myfield.disableValidate);
if (myfield.disableValidate==true) {
if (myfield.getValue().length == 0) {
this.markInvalid(Ext.form.TextField.prototype.blankText);
return false;
}
this.clearInvalid();
return true;
}
else {
var clientValid = validate.call(this);
if(!this.disabled && !clientValid) {
return false;
}
if(this.disabled || clientValid ) {
this.clearInvalid();
return true;
}
return false;
}
}
});

field.on({
render:{single:true, scope:field, fn:function() {
this.validateOnBlur = false;
}}
});
}
};usage:


var targa = new Ext.form.TextField({
fieldLabel: 'Targa',
id: 'txtTarga',
width: 80 ,
name: 'targa',
minLength: 7,
maxLength: 7,
allowBlank:false
,regex:/^[a-zA-Z][a-zA-Z][0-9][0-9][0-9][a-zA-Z][a-zA-Z]$/
,regexText: 'Formato targa non valido'
,disableValidate:false
,plugins:[Ext.ux.plugins.DisableValidator]

});

...


showTarga: function(b) {
if(b){
targa.disableValidate = true;
targa.validate();
}
else {
targa.disableValidate = false;
targa.validate();
}
}