nhibernate.validator 和 x.Val& 带有自定义验证器的 jQuery

发布于 2024-07-29 21:29:45 字数 2039 浏览 7 评论 0原文

我一直在使用 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"}]},

它附加了所需的字段验证器,但不是定制一.

谁能帮我这个? 这是一个非常关键的功能!

谢谢你!

I've been playing with nhibernate.validator and xVal and JQuery and they work together quite nicely, until I try to have custom validators. According to the xVal codeplex side custom validators are supported if they implement the ICustomRule interface. and you supply the ToCustomRule function which returns a customRule with the name of the Javascript function that will do the client side validation.

My validator is being used on the server side, but it isn't getting attached to the field on the client side.

here are the important parts of the code:

The property that is being validated:


 _
    Public Property Password() As String
        Get
            Return m_Password
        End Get
        Set(ByVal value As String)
            m_Password = value
        End Set
    End Property

The Custom Validator:


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

and this isthe important part of what is generated in the source by the html.ClientSideValidation(of user)


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

It's attaching the required field validator but not the custom one.

Can anyone help me with this? It's a pretty key bit of functionality!

Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇女薄笑 2024-08-05 21:29:45

确保您不调用xVal.ActiveRuleProviders.Providers.Clear(),或者如果您调用,请确保像这样添加 CustomRulesProvider xVal.ActiveRuleProviders.Providers.Add(new xVal.RuleProviders. CustomRulesProvider())

Ensure you don´t callxVal.ActiveRuleProviders.Providers.Clear() or if you do, then ensure you add CustomRulesProvider like this xVal.ActiveRuleProviders.Providers.Add(new xVal.RuleProviders.CustomRulesProvider())

你好,陌生人 2024-08-05 21:29:45

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

I ended up ditching this and using some remote validation rules that were added in the newest version of XVal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文