2016-04-26 50 views
2

我得到了一個帶有用戶名文本框的註冊表單。 對於這個文本字段我添加了一個自定義驗證函數來檢查的可用性所選用戶名:ExtJS 6:文本框上的驗證器無法正常工作

      xtype: 'textfield', 
          name: 'username', 
          msgTarget: 'under', 
          bind: { 
           fieldLabel: '{texts.username}' 
          }, 
          allowBlank: false, 
          minLength: 3, 
          checkChangeBuffer: 300, 
          validator: function (value) { 
           if (value.length < 3) { 
            return null; 
           } 
           Ext.Ajax.request({ 
            method: 'POST', 
            url: '/rest/availability', 
            params: { 
             name: value 
            } 
           }).then(
            function (result) { 
             return Ext.JSON.decode(result.responseText) ? true : 'Username already exists'; 
            }, 
            function (response) { 
             return 'Server issue.'; 
            } 
           ) 
          } 

這也許應該這樣做。但它沒有。 我得到一個破碎的錯誤,驗證總是顯示無效,不顯示消息:

textfield - user exists

當我檢查了迴應,我從服務器獲取正確的值。 我在這裏錯過了什麼?

在此先感謝。

+2

參見:http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 –

+2

參見:HTTP:// stackoverflow.com/a/8121254 –

+0

非常感謝。我想我忽略了這個.. – xdn

回答

0

爲了驗證通過,您需要返回true。

validator: function(value) { 
    if(!valid) { 
     return 'error message'; 
    } 
    return true; // successfully passed 
} 
+0

謝謝!但我已經使用了舊帖子的答案。我會盡快更新我的答案! – xdn