C
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
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