PDA

View Full Version : Scope of textfield's validator,and a prblem about ajax validator



safewolf
23 Dec 2010, 1:10 AM
Hi, everyone

i have problem now.

You know, when i add a listener to the textfield, i can set the scope like this:



textfield.on(“blur”,handler,scope);



but in the form panel, i want to add a validator to the textfield, and want to set the listener's scope to the panel
so , this is my code



MyPanel = function() {
this.validator = function(value) {
Ext.Ajax.request({
url : myUrl,
method : 'POST',
params : {
account : value,
guid : this.getForm().findField("guid").getValue()
},
success : function(response, action) {
},
failure : function(response, options) {
}
});
return true;
}

MyPanel.superclass.constructor.call(this, {
items : [{
xtype : 'hidden',
name : 'guid',
value : 'this is guid',
invalidText : 'Invalid?'
}, {
xtype : 'textfield',
name : 'name',
validationEvent : 'blur',
validator : this.validator
}]

});

}

Ext.extend(MyPanel, Ext.FormPanel, {});




this code will report this.getForm() is undefined,
and by using firebug, "this" here is the textfield

so how can i use this.getForm() here?

and another problem:
i want to validate the field by using ajax request
how can i validate the field?
because the validator function can't return the result from the ajax response

Condor
23 Dec 2010, 1:19 AM
'this' will be the field and not the form, unless you override the scope using

validator: this.validator.createDelegate(this)

safewolf
23 Dec 2010, 1:33 AM
thanks very much!

But, do you know how to do the ajax validation?
the valiadtor function can't return result from the ajax result
How should i do can make the function return the correct value?

Condor
23 Dec 2010, 1:36 AM
Have a look at the RemoteValidator plugin (see User Extensions forum).

safewolf
23 Dec 2010, 1:50 AM
thanks !

Troy Wolf
18 Nov 2011, 10:34 AM
I try to architect my application objects in such a way that the scope is natural, but it never fails, if the app has any complexity at all, I end up needing to fudge some things here and there. The way I've solved the problem described by safewolf is to create the reference I need upon object instantiation.

You can add whatever custom properties you want to any object. In the example, below, the current object scope (this) is what I know I'll want to access later in my validator function. Instead of 'this', you could put any object you want to access later.


...
name: 'myTextfield',
parentScope: this,
width: 200,
...

Then, for example, when my field validator function is called and 'this' is the textfield, I can do something like this:


this.parentScope.foo();

If this is wrong, I don't want to be right. B)