我一直在玩nhibernate.validator和xVal和JQuery和他們一起工作很好,直到我嘗試有自定義驗證。根據xVal codeplex側面的自定義驗證器,如果它們實現了ICustomRule接口,則會受到支持。並提供ToCustomRule函數,該函數返回一個customRule,其中包含將執行客戶端驗證的Javascript函數的名稱。nhibernate.validator&x.Val&jQuery與自定義驗證器
我的驗證器正在服務器端使用,但它沒有連接到客戶端的字段。
這裏是代碼的重要組成部分:
正在被驗證的屬性:
_
Public Property Password() As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = value
End Set
End Property
自定義驗證:
Imports NHibernate.Validator.Engine
Imports xVal.RuleProviders
Imports xVal.Rules
_
_
Public Class PasswordValidationAttribute
Inherits Attribute
Implements IRuleArgs
Private m_Message As String = "Password and Confirm Password must be the same"
Public Property Message() As String Implements NHibernate.Validator.Engine.IRuleArgs.Message
Get
Return m_Message
End Get
Set(ByVal value As String)
m_Message = value
End Set
End Property
End Class
Public Class PasswordValidator
Implements IValidator, ICustomRule
Public Function IsValid(ByVal value As Object) As Boolean Implements NHibernate.Validator.Engine.IValidator.IsValid
Dim valid As Boolean = True
Dim val As String = CType(value, String)
If val = "hello" Then
valid = True
Else
valid = False
End If
Return valid
End Function
Public Function ToCustomRule() As xVal.Rules.CustomRule Implements xVal.RuleProviders.ICustomRule.ToCustomRule
Return New CustomRule("ValidatePassword", Nothing, "Password and Password Confirmation must Match")
End Function
End Class
這isthe什麼是重要的組成部分由源代碼生成的html.ClientSideValidation(用戶)
{"FieldName":"Password","FieldRules":[{"RuleName":"Required","RuleParameters":{},"Message":"Password is Required"}]},
它附加了所需的字段驗證程序,但不包含自定義字段。
任何人都可以幫助我嗎?這是一個非常關鍵的功能!
謝謝!