Custom Validator not working without RequiredValidator

  • Thread starter Elie Medeiros via .NET 247
  • Start date
E

Elie Medeiros via .NET 247

Hi there,

I have written a custom validator function to validate a datefrom a user-filled field. The function tries to parse the dateand if it can't, sets (serverValidateEventArgs)e.IsValid tofalse.

The twist is that it allows blank dates to be entered - viz,e.IsValid==true if the field is left blank. The idea is to usethat validation control to make sure the date a user has enteredis valid, without the date field being required to complete theform.

The problem is that this doesn't work - although theCustomValidator itself does not show an error message when thefield is left blank, the ValidationSummary does show the errormessage, and Page.IsValid==false. The most troubling is that Iadded some logging code to the validation function which doesn'tseem to get called, suggesting that the validation functiondoesn't actully get called...


The validation function is as follows:

----------------

public static void ValidateShortDate(object sender,ServerValidateEventArgs e)
{
logger.Debug("Trying to validate a date");
if(StringUtil.IsEmpty(e.Value))
{
e.IsValid=true;
logger.Debug("Date to validate is empty, returningvalid=true");
return;
}

DateTime dt = DateTime.MinValue;
try
{
dt = DateTime.ParseExact(e.Value.Trim(), "d",CultureInfo.CurrentCulture);
}
catch (Exception ex)
{
e.IsValid = false;
logger.Debug("Error parsing date to validate, returningvalid=false", ex);
return;
}
if (dt.Equals(DateTime.MinValue))
{
e.IsValid = false;
logger.Debug("Date to validate==DateTime.MinValue, returningvalid=false");
return;
}
logger.Debug("Date to validate is valid ("+dt+"), returningvalid=true");
e.IsValid = true;
}


----------------


and in the aspx page:

<asp:TextBox runat="server" id="_dateOfBirthCalendar" />
<!--
<asp:RequiredFieldValidator ID="_dateOfBirthValidator1"ControlToValidate="_dateOfBirthCalendar" Runat="server"
ErrorMessage="Please enter a valid birth date" />
-->
<asp:CustomValidator ID="_dateOfBirthValidator"ControlToValidate="_dateOfBirthCalendar"
Runat="server" ErrorMessage="Please enter a valid birth date"/>

----------------


I replaced a custom control by the TextBox in this example.

Note that uncommenting the RequireValidator, the code performs asexpected (ie an error messgage appears both next to the fieldand in the ValidatorSummary)

Any help greatly appreciated.
 
W

William F. Robertson, Jr.

Try removing the ControlToValidate property from your CustomValidator. If
the ControlToValidate is left blank, the validator will fire all the time
without working with the RequiredFieldValidator.

You might want to add your own attribute that defines the ControlToValidate
so you know what control to check on the server.

HTH,

bill


Hi there,

I have written a custom validator function to validate a date from a
user-filled field. The function tries to parse the date and if it can't,
sets (serverValidateEventArgs)e.IsValid to false.

The twist is that it allows blank dates to be entered - viz, e.IsValid==true
if the field is left blank. The idea is to use that validation control to
make sure the date a user has entered is valid, without the date field being
required to complete the form.

The problem is that this doesn't work - although the CustomValidator itself
does not show an error message when the field is left blank, the
ValidationSummary does show the error message, and Page.IsValid==false. The
most troubling is that I added some logging code to the validation function
which doesn't seem to get called, suggesting that the validation function
doesn't actully get called...


The validation function is as follows:

----------------

public static void ValidateShortDate(object sender, ServerValidateEventArgs
e)
{
logger.Debug("Trying to validate a date");
if(StringUtil.IsEmpty(e.Value))
{
e.IsValid=true;
logger.Debug("Date to validate is empty, returning valid=true");
return;
}

DateTime dt = DateTime.MinValue;
try
{
dt = DateTime.ParseExact(e.Value.Trim(), "d",
CultureInfo.CurrentCulture);
}
catch (Exception ex)
{
e.IsValid = false;
logger.Debug("Error parsing date to validate, returning valid=false", ex);
return;
}
if (dt.Equals(DateTime.MinValue))
{
e.IsValid = false;
logger.Debug("Date to validate==DateTime.MinValue, returning valid=false");
return;
}
logger.Debug("Date to validate is valid ("+dt+"), returning valid=true");
e.IsValid = true;
}


----------------


and in the aspx page:

<asp:TextBox runat="server" id="_dateOfBirthCalendar" />
<!--
<asp:RequiredFieldValidator ID="_dateOfBirthValidator1"
ControlToValidate="_dateOfBirthCalendar" Runat="server"
ErrorMessage="Please enter a valid birth date" />
-->
<asp:CustomValidator ID="_dateOfBirthValidator"
ControlToValidate="_dateOfBirthCalendar"
Runat="server" ErrorMessage="Please enter a valid birth date" />

----------------


I replaced a custom control by the TextBox in this example.

Note that uncommenting the RequireValidator, the code performs as expected
(ie an error messgage appears both next to the field and in the
ValidatorSummary)

Any help greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top