2016-02-09 85 views
0

在以下驗證用戶名具有值並且密碼匹配的代碼中,即使正在執行驗證規則,onSuccess和onFailure回調也不會被觸發/執行。我已經測試了這個獨立的其他部分,並且這個佈局完全遵循了文檔,所以我排除了語法錯誤。除了以下使用的語法外,我還嘗試了格式$().form()。onSuccess()。onFailure(),但得到了「Uncaught TypeError:onSuccess不是函數」。這裏撥動:Semantic UI onSuccess, onFailure callback example語義UI onSuccess,onFailure回調沒有在表單驗證時被觸發

$(function(){ 
    $('#main') 
     .form({ 
      on: 'blur', 
      fields: { 
       username: { 
        identifier : 'username', 
        rules: [ 
         { 
          type : 'empty' 
         } 
        ] 
       }, 
       password1: { 
        identifier : 'password1', 
        rules: [ 
         { 
          type : 'match[password2]', 
          prompt : 'Please enter the same value in both fields' 
         } 
        ] 
       }, 
       password2: { 
        identifier : 'password2', 
        rules: [ 
         { 
          type : 'match[password1]', 
          prompt : 'Please enter the same value in both fields' 
         } 
        ] 
       } 
      }, 
      onSuccess: function() { 
       $(".ui.button[name='account']").removeClass('disabled'); 
       console.log('Success'); 
      }, 
      onFailure: function() { 
       $(".ui.button[name='account']").addClass('disabled'); 
       console.log('Failure'); 
      } 
     } 
    ); 
}); 
+0

我剛剛更新的代碼上的jsfiddle https://jsfiddle.net/6y7w57xv/3/ –

回答

2

關鍵是這樣調用它。 $('#main').form(fieldRules, settings); onFailureonSuccess已在設置中註冊。

看一看的修改後的代碼:

$(function(){ 
     $('#main') 
      .form({ 
        username: { 
         identifier : 'username', 
         rules: [ 
          { 
           type : 'empty' 
          } 
         ] 
        }, 
        password1: { 
         identifier : 'password1', 
         rules: [ 
           { 
            type: 'empty', 
           prompt: 'Please enter the password' 
           }, 
          { 
           type : 'match[password2]', 
           prompt : 'Please enter the same value in both fields' 
          } 
         ] 
        }, 
        password2: { 
         identifier : 'password2', 
         rules: [ 
          { 
           type : 'match[password1]', 
           prompt : 'Please enter the same value in both fields' 
          } 
         ] 
        } 
       }, { 
        on: 'blur', 
        inline: true, 
        onSuccess: function() { 
        alert('Success'); 
        return false; // false is required if you do don't want to let it submit 

        }, 
        onFailure: function() { 
        alert('Failure'); 
        return false; // false is required if you do don't want to let it submit            
        } 
        }); 
    }); 
+0

這並不在所有的工作 - 驗證甚至不運行。加上第一個對象的'on'和'inline'工作得很好,那爲什麼不'onSuccess'和'onFailure'呢?我確保刪除第一個對象中的包裝字段:{}',但仍然沒有任何反應。 –

+0

啊哈!至少在最新版本(2.2.x)中,您不*使用第二個設置對象。 OP實際上已經有了正確的代碼 - 問題在於'onSuccess'和'onFailure'回調有一個不直觀的行爲,特別是在我使用單字段表單的情況下。當基於'on'方法完成驗證時,它們不會被**調用。即:'on:'blur'或'on:'change'並不意味着只要最後一個字段有效,就會調用onSuccess。你*必須*提交表單,或者在單字段表單的情況下,使用'onValid'事件。 –

0

我就遇到了這個問題爲好。我什麼都試過,我的onSuccess火災的唯一方法是這樣:

const formValidated = $('.ui.form').form(
    { 
     fields: { 
      name: { 
       identifier: 'name', 
       rules: [ 
       { 
        type : 'empty', 
        prompt : 'Please enter a name for the camera' 
       } 
       ] 
      }, 
     } 
    } 
) 

    if ($(formValidated).form('is valid')) { 
    this.handleSave(); 
    } else { 
    return false; 
    } 
相關問題