nhibernate.validator& x.Val& jQuery与自定义验证器

时间:2009-08-06 18:58:15

标签: jquery asp.net-mvc validation xval

我一直在玩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(用户)在源代码中生成的内容的重要部分


{"FieldName":"Password","FieldRules":[{"RuleName":"Required","RuleParameters":{},"Message":"Password is Required"}]},

它附加了必需的字段验证器,但不附加自定义验证器。

任何人都可以帮我吗?这是一个非常关键的功能!

谢谢!

2 个答案:

答案 0 :(得分:0)

确保您没有拨打xVal.ActiveRuleProviders.Providers.Clear(),或者如果您这样做,请确保添加CustomRulesProvider,如xVal.ActiveRuleProviders.Providers.Add(new xVal.RuleProviders.CustomRulesProvider())

答案 1 :(得分:0)

我最终放弃了这一点,并使用了最新版本的XVal中添加的一些远程验证规则。