2015-10-04 23 views
3

我一直在嘗試使模式登錄表單,但作爲證書驗證完成後按提交對話框被關閉,當我再次打開它時,我看到錯誤 - 無效用戶名或密碼。使用web2py模式處理登錄表單

這裏是我的jQuery-

$(document).ready(function() { 

$('#loginForm').formValidation({ 
    framework: 'bootstrap', 
    icon: { 
     valid: 'glyphicon glyphicon-ok', 
     invalid: 'glyphicon glyphicon-remove', 
     validating: 'glyphicon glyphicon-refresh' 
    }, 
    fields: { 
     username: { 
      validators: { 
       notEmpty: { 
        message: 'The username is required' 
       } 
      } 
     }, 
     password: { 
      validators: { 
       notEmpty: { 
        message: 'The password is required' 
       } 
      } 
     } 
    } 
}); 

我能得到任何基本功能的實現模式登錄,這是使用AJAX,其中模式對話框關閉僅在成功登錄。

+0

這個例子很有幫助:http://formvalidation.io/examples/modal/ – Arkni

回答

0

是的,你可以。雖然我沒有一個完整的工作示例(因爲我沒有自己的框架副本)。

查看remote validator,它提供了你想要的。儘管你必須在web2py方面有兩個驗證器。使用sending dynamic data示例在檢查密碼時發送用戶名和密碼。

類似的服務器端使用代碼:

def test_email(): 
    found = db(db.auth_user.email == request.post_vars.email).count() 
    return response.json(dict(valid=found > 0)) 


def test_credentials(): 
    records = db(db.auth_user.email == request.post_vars.email).select() 
    if not records: 
     return response.json(dict(valid=False)) 
    else: 
     hashed_password = db.auth_user.password.validate(request.post_vars.password)[0] 
     hashes_equal = records.first().password == hashed_password 
     return response.json(dict(valid=hashes_equal)) 

(請注意web2py會默認爲電子郵件,而不是用戶名,所以我用的是不是你可以輕鬆地改變你的字段名。)

或者改變從documentation found here樣品中,您可以在結果的// do you logic部分開關執行一些AJAX收到有你的模式正確響應:

(function($) { 
    FormValidation.Validator.validatorName = { 
     validate: function(validator, $field, options) { 
      // ... Do your logic checking 
      if (...) { 
       return { 
        valid: true, // or false 
        message: 'The error message' 
       } 
      } 

      return { 
       valid: false,  // or true 
       message: 'Other error message' 
      } 
     } 
    }; 
}(window.jQuery)); 

這種方法允許其他庫,如jquery-jsonrpc2.0發光和緩解發展。