我一直在使用 nhibernate.validator 和 xVal 和 JQuery,它们可以很好地协同工作,直到我尝试使用自定义验证器。根据 xVal codeplex,如果实现 ICustomRule 接口,则支持自定义验证器。并且您提供 ToCustomRule 函数,该函数返回一个 customRule,其中包含将执行客户端验证的 Javascript 函数的名称。
我的验证器正在服务器端使用,但它没有附加到客户端的字段。
以下是代码的重要部分:
正在验证的属性:
_
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
这是 html.ClientSideValidation(of user) 在源代码中生成的重要部分
{"FieldName":"Password","FieldRules":[{"RuleName":"Required","RuleParameters":{},"Message":"Password is Required"}]},
它附加了必需的字段验证器,但不是自定义的。
谁能帮我这个?这是一个非常关键的功能!
谢谢你!