Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all,

 

There seems to be a complete black hole in the net when it comes to information about subclassing BaseValidator..

 

I have a webform with a bunch of textboxes I need to validate. Some are required text fields, others are date fields that are required, must be valid dates, and are in an expected format.

 

I'm using the RequiredFieldValidator for the first and decided to create my own Validator for the second type, the DateValidator.

 

The EvaluateIsValid method of my DateValidator simply checks if the value exists and is a valid date (and I plan on using a regexp to check the format).

 

The problems are these:

1) My DateValidator works and displays it's ErrorMessage when appropriate, but does not appear in a ValidationSummary list with ShowMessageBox=true

 

2) If i have any other standard validator present on the page (eg. the previously mentioned RequiredFieldValidator) that validator is processed first and, if it returns IsValid=false, the DateValidator does not validate. As far as I can tell, the EvaluateIsValid method is never called.

 

I've spent a day looking for the missing bracket or closing tag but I just can't seem to figure it out..

 

If no one else has had similar problems, I'll post the code and see if someone can reproduce it.

 

 

Many thanks..

 

Fade

(the rapidly balding..)

Posted

Having thought about it further (and when it's not 6pm on a friday..) I wondered if maybe there's some kind of order that validators get processed.. ie. if a RequiredField validator fails then no other validators are processed?? that would answer question 2 but still not the effect I hoped for since I essentially want to validate that a date is entered AND valid.

 

Either way, the error message from my subclassed validator shows in the validation list, but not in the message box.

 

here's the page (stripped down as much as possible):

 

<%@ register tagprefix="cms" namespace="CustomValidators" Assembly="CMSWebLib" %>

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="CMS.WebForm1"%>

<HTML>

<HEAD>

<title>WebForm1</title>

</HEAD>

<body>

<form id="Form1" runat="server">

<table cellSpacing="0" cellPadding="2">

<tr>

<td>

<asp:textbox id="txtText1" runat="server" BackColor="White"></asp:textbox>

<cms:DateValidator id="Datevalidator1" runat="server" display="None" ControlToValidate="txtText1" ErrorMessage="Invalid Date 1"></cms:DateValidator></td>

<td>

<asp:textbox id="txtText2" runat="server"></asp:textbox>

<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" Display="None" ControlToValidate="txtText2" ErrorMessage="Missing Date 2"></asp:RequiredFieldValidator></td>

<td>

<asp:textbox id="txtText3" runat="server" Width="100px"></asp:textbox>

<cms:DateValidator id="DateValidator2" runat="server" display="None" ControlToValidate="txtText3" ErrorMessage="Invalid Date 3"></cms:DateValidator>

</td>

</tr>

<tr>

<td>Date 1</td>

<td>Date 2</td>

<td>Date 3</td>

</tr>

<tr>

<td>

<asp:button runat="server" text="Validate" ID="btnValidate" />

</td>

</tr>

</table>

<asp:ValidationSummary id="ValidationSummary1" runat="server" ShowMessageBox="True"></asp:ValidationSummary>

</form>

</body>

</HTML>

 

and the Validator object defined in my CMSWebLib dll:

 

Imports System

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Text

 

Namespace CustomValidators

 

Public Class DateValidator

Inherits BaseValidator

 

Protected Overrides Function EvaluateIsValid() As Boolean

 

Dim strInputDate As String

 

strInputDate = Me.GetControlValidationValue(Me.ControlToValidate)

 

If Trim(strInputDate <> "") AndAlso IsDate(strInputDate) Then

Return True

Else

Return False

End If

 

End Function

 

End Class

End Namespace

Posted
Trim(strInputDate <> "") returns string, so it is quite incorret to put it in "if" statement. Maybe thats the point. BTW Trim() is VB time, shoud be "if (strInputDate.Trim.Length > 0)..."

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted

hehe ok so a bracket was out of place after all.. trim(strInputDate) <> "" was what it was supposed to be (yes, spot the VB programmer) and would have worked too.. the .length property looks neater, but would not catch whitespace so .Trim.Length sounds good. Thanks hrabia!

 

Unfortunately that doesn't solve the problem.. In the sample above if you leave all 3 textboxes blank, only the message from the RequiredFieldValidator is displayed.

 

If a value is entered in the required field, the two DateValidators fire and correctly report invalid dates.. but only to the on-page summary list and not in the messagebox..

Posted
I think that your problem is where the validate occurs. In server controls validators default value of EnableClientScript is true, that means when you use IE4 or better fields are validate on client (javascript functions). That the reason why only RequiredFieldValidator occurs in MessageBox and summary list. You can set this property to false (RequiredFieldValidator2.EnableClientScript = False) and after that, all validation will be made on server. You can also emitt your own client side validate script but that no so easy, look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconclient-sidefunctionalityinservercontrol.asp or you can write your own base validator implementing IValidator interface.

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted

Having thought that through it makes a lot of sense..

 

Emitting your own script does look like a pretty involved process and is something I'd like to look into but I don't think I've got enough time before my deadlines so I'll switch everything over to server-side validation (for the sake of consistancy).

 

Thanks for all the help, Hrabia. If I have any luck with emitting client-side validation code in the future I'll post it here!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...