Extjs4 - remote validation

I'd like having a remote validator for a textfield. My PHP returns true/false value. I've tried something like this:

{
   xtype: 'textfield',
   fieldLabel: 'Field',
   allowBlank: false,
   validator : function(value) {
      Ext.Ajax.request({
      url: 'psc/validate',
      params: { psc: value },
      success: function(response){
       return response.responseText                             
      }
      });
   });
}

The problem is that ajax request is asynchonous and the validator gives "values not defined" error. Is there any callback? So I would return false by default and make textfield valid once ajax call would be finished.

I've tried to google for extjs remote validation but there is not much about it.

Anybody help or suggestions? Thanks.

Answers


maybe you shouldnt use the validator then, add a listner on change for the textfield and use the methods markInvalid and clearInvalid for displaying the validation.

{
   xtype: 'textfield',
   fieldLabel: 'Field',
   allowBlank: false,
   textValid: false,
   validator: function(){
       return this.textValid;
   },
   listeners : {
     'change': function(textfield,newValue,oldValue) {
        Ext.Ajax.request({
          url: 'psc/validate',
          params: { psc: value },
          scope: textfield,
          success: function(response){
             if (response.responseText){
               this.clearInvalid();
               this.textValid = true;
             } else {
               this.markInvalid('field is not valid');
               this.textValid = false;
             }                             
          }
        });
      }       
   }
}

I haven;t tried it but could work for your aproach

EDIT i've made some modifications to the code to include the validator..


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.