Ad

Nhibernate.validator & X.Val & JQuery With Custom Validators

- 1 answer

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!

Ad

Answer

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

Ad
source: stackoverflow.com
Ad