Home All Groups Group Topic Archive Search About

ASP.Net 2.0 Validation Groups & IValidator Controls.

Author
25 Sep 2006 11:11 AM
Craig
Ok so ASP.Net 2.0 Validation Groups are great and lovely...

But has anyone managed to sort out a solution to Validators that directly
Implement IValidator (as opposed to inheriting from BaseValidator) error
message's not appearing in the ValidationSummary.

Starting point for my control was....
http://codeproject.com/aspnet/selfvalidatingtextbox.asp,
http://www.codetools.com/aspnet/SelfValidatingTextBox2.asp

I've overridden the Validate method and GetValidators on the page to work
with validationGroups...the default page ones cast all registered validators
to BaseValidator....

But validationSummary doesn't want to play ball..Reflecting the
ValidationSummary.GetErrorMessages :

Dim collection1 As ValidatorCollection =
Me.Page.GetValidators(Me.ValidationGroup)

But this isn't firing my custom Page.GetValidators call....which I find odd...

My workaround at the moment is to copy the reflected code of
ValidationSummary into a customValidationSummary control and alter that so
that it grabs the method via reflection...

'Dim collection1 As ValidatorCollection =
Me.Page.GetValidators(Me.ValidationGroup)

        Dim collection1 As ValidatorCollection
        Dim oMethodInfo As System.Reflection.MethodInfo =
Me.Page.GetType.GetMethod("GetValidators")
        collection1 = oMethodInfo.Invoke(Me.Page, New Object()
{Me.ValidationGroup})

        Dim collection2 As ValidatorCollection =
Me.Page.GetValidators(Me.ValidationGroup)

This isn't that elegant so I thought I'd pitch for other
suggestions...(apart from, Create a baseValidator inherited validation
control...and replace the single self validating control with a textbox and
the new validation control.)

Cheers..

C.


Public Overrides Sub Validate(ByVal validationGroup As String)
        MyBase.Validate(validationGroup)
        Dim vals As ValidatorCollection = GetValidators(validationGroup)
        If String.IsNullOrEmpty(validationGroup) AndAlso Me.Validators.Count
= vals.Count Then
            Me.Validate()
        Else
            Dim i As Integer = 0
            Do While (i < vals.Count)
                vals.Item(i).Validate()
                i += 1
            Loop

        End If

    End Sub

    Public Overloads Function GetValidators(ByVal validationgroup As String)
As ValidatorCollection
        Dim coll As New ValidatorCollection

        For Each obj As Object In Page.Validators
            Dim val As BaseValidator = TryCast(obj, BaseValidator)

            If Not val Is Nothing AndAlso
String.Compare(val.ValidationGroup, validationgroup,
StringComparison.Ordinal) = 0 Then
                coll.Add(val)
            Else

                Dim ival As IValidator = TryCast(obj, IValidator)
                If Not ival Is Nothing Then

                    'IValidator dsoesn't expose validation group
                    Dim objtype As Type = obj.GetType()

                    Dim prop As System.Reflection.PropertyInfo =
objtype.GetProperty("ValidationGroup", System.Reflection.BindingFlags.Public
Or System.Reflection.BindingFlags.Instance)
                    Dim group As String = prop.GetValue(obj,
Nothing).ToString()

                    If String.Compare(group, validationgroup,
StringComparison.Ordinal) = 0 Then
                        coll.Add(ival)
                    End If

                End If


            End If

        Next

        Return coll
    End Function

Author
28 Sep 2006 2:00 PM
Gaurav Vaish (www.EduJiniOnline.com)
> Implement IValidator (as opposed to inheriting from BaseValidator) error
> message's not appearing in the ValidationSummary.

Have you checked that the ValidationGroup of ValidationSummary is matching?


Author
28 Sep 2006 2:04 PM
Gaurav Vaish (www.EduJiniOnline.com)
Ignore my previous mail...
    for all non-BaseValidator derived validators, it adds only if the
ValidationGroup of ValidationSummary is string.Empty.

    Not sure if it has already been reported as a bug or a known-issue or a
feature-as-is.

Show quoteHide quote
"Craig" <Cr***@discussions.microsoft.com> wrote in message
news:E74E5F13-EBDA-4A64-A8B0-8530FFCD3C68@microsoft.com...
> Ok so ASP.Net 2.0 Validation Groups are great and lovely...
>
> But has anyone managed to sort out a solution to Validators that directly
> Implement IValidator (as opposed to inheriting from BaseValidator) error
> message's not appearing in the ValidationSummary.
>
> Starting point for my control was....
> http://codeproject.com/aspnet/selfvalidatingtextbox.asp,
> http://www.codetools.com/aspnet/SelfValidatingTextBox2.asp
>
> I've overridden the Validate method and GetValidators on the page to work
> with validationGroups...the default page ones cast all registered
> validators
> to BaseValidator....
>
> But validationSummary doesn't want to play ball..Reflecting the
> ValidationSummary.GetErrorMessages :
>
> Dim collection1 As ValidatorCollection =
> Me.Page.GetValidators(Me.ValidationGroup)
>
> But this isn't firing my custom Page.GetValidators call....which I find
> odd...
>
> My workaround at the moment is to copy the reflected code of
> ValidationSummary into a customValidationSummary control and alter that so
> that it grabs the method via reflection...
>
> 'Dim collection1 As ValidatorCollection =
> Me.Page.GetValidators(Me.ValidationGroup)
>
>        Dim collection1 As ValidatorCollection
>        Dim oMethodInfo As System.Reflection.MethodInfo =
> Me.Page.GetType.GetMethod("GetValidators")
>        collection1 = oMethodInfo.Invoke(Me.Page, New Object()
> {Me.ValidationGroup})
>
>        Dim collection2 As ValidatorCollection =
> Me.Page.GetValidators(Me.ValidationGroup)
>
> This isn't that elegant so I thought I'd pitch for other
> suggestions...(apart from, Create a baseValidator inherited validation
> control...and replace the single self validating control with a textbox
> and
> the new validation control.)
>
> Cheers..
>
> C.
>
>
> Public Overrides Sub Validate(ByVal validationGroup As String)
>        MyBase.Validate(validationGroup)
>        Dim vals As ValidatorCollection = GetValidators(validationGroup)
>        If String.IsNullOrEmpty(validationGroup) AndAlso
> Me.Validators.Count
> = vals.Count Then
>            Me.Validate()
>        Else
>            Dim i As Integer = 0
>            Do While (i < vals.Count)
>                vals.Item(i).Validate()
>                i += 1
>            Loop
>
>        End If
>
>    End Sub
>
>    Public Overloads Function GetValidators(ByVal validationgroup As
> String)
> As ValidatorCollection
>        Dim coll As New ValidatorCollection
>
>        For Each obj As Object In Page.Validators
>            Dim val As BaseValidator = TryCast(obj, BaseValidator)
>
>            If Not val Is Nothing AndAlso
> String.Compare(val.ValidationGroup, validationgroup,
> StringComparison.Ordinal) = 0 Then
>                coll.Add(val)
>            Else
>
>                Dim ival As IValidator = TryCast(obj, IValidator)
>                If Not ival Is Nothing Then
>
>                    'IValidator dsoesn't expose validation group
>                    Dim objtype As Type = obj.GetType()
>
>                    Dim prop As System.Reflection.PropertyInfo =
> objtype.GetProperty("ValidationGroup",
> System.Reflection.BindingFlags.Public
> Or System.Reflection.BindingFlags.Instance)
>                    Dim group As String = prop.GetValue(obj,
> Nothing).ToString()
>
>                    If String.Compare(group, validationgroup,
> StringComparison.Ordinal) = 0 Then
>                        coll.Add(ival)
>                    End If
>
>                End If
>
>
>            End If
>
>        Next
>
>        Return coll
>    End Function
>
Author
10 Nov 2006 5:39 PM
Carl Reid
Have you found a solution to this problem? I am stuck on exactly this issue and it looks like I have got pretty much the same code.

what I don't understand is why the call to Page.GetValidators still calls the system.web.ui.page method instead of the overridden method in my custom page (which inherits from system.web.ui.page). Any more information would be invaluable.