ASP.NET PropertyProxyValidator with type conversion

Topics: Validation Application Block
Oct 16, 2007 at 10:48 AM
I'm using the PropertyProxyValidator control to validate a textbox input as a datetime using OnValueConvert to check if a user input is a valid datetime. And thats is basically the only rule, i.e. I have not added any Validators to the Date property:

public class Customer
private DateTime _Date;
public DateTime Date
get { return _Date; }
set { _Date = value; }

public Customer()

I have declared the PropertyProxyValidator like this:

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<cc1:PropertyProxyValidator ID="PropertyProxyValidator2" runat="server" ControlToValidate="TextBox2" PropertyName="Date" SourceTypeName="Customer" OnValueConvert="TextBox2_ValueConvert"></cc1:PropertyProxyValidator>

In the ValueConvert I simply check if the text is a datetime and convert it.

This does NOT work! Why?

It seems like you must have at least one validator added to the property, but I do not need any other rules, if it can convert to a valid datetime, it is fine. If I add a TypeConversionValidator(typeof(DateTime)) attribute (or bascially any other attribute), it works fine. But adding this attribute does not really make any sense since the Property is a DateTime so I can only "set" DateTime values.

The "culprit" as far as I can see is the PropertyProxyValidator.EvaluateIsValid() method:

protected override bool EvaluateIsValid()
Validator validator = new ValidationIntegrationHelper(this).GetValidator();

if (validator != null)
ValidationResults validationResults = validator.Validate(this);

this.ErrorMessage = FormatErrorMessage(validationResults, this.DisplayMode);
return validationResults.IsValid;
this.ErrorMessage = "";
return true;

Where the validator IS null in my case, so everything is a-ok even though the type conversion might fail...

Can this really be true???

Of course we could always just make sure that the TypeConversionValidator is added, but I know that other developers in my department will forget it (not me of course ;))
Oct 16, 2007 at 3:01 PM
I haven't gotten into the ASP.NET PropertyProxyValidator but I have to agree with you the conversion handling is a little shady. The fact that there's a Convert event is a bit suspect. At the very least it should try to call TypeDescriptor.GetConverter() and attempt conversion before calling an event that does this.

But I'm just conjecturing. I should hold my tongue until I actually try this thing out. :)
Oct 17, 2007 at 11:38 AM
Edited Oct 17, 2007 at 12:39 PM
Actually I just tried without the OnValueConvert set and just added a TypeConversionValidator(typeof(DateTime)) attribute to my Date. Then I get this strange message:

Value to validate is not of the expected type: expected System.String but got System.DateTime instead.

This seems to come from the Validator<T> (Validator.Generic.cs) class - this makes less and less sense to me :) But perhaps I'm just missing the point!

edit Maybe I should just clarify that the error is shown as the Error Message if the validator on the page
Oct 19, 2007 at 8:19 AM
There is definitely something wrong here!

If you run the ValidationAspNetQuickStart the Customer class has a RelativeDateTimeValidator on the DateOfBirth property. There is a PropertyProxyValidator and type conversion similar to what I did.

The property is:

RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, Ruleset="RuleSetA", MessageTemplate="Must be 18 years or older.")
public DateTime DateOfBirth
{ ...

I.e. it is a RuleSetA rule.

If you run the app and enter "abcd" in the DateOfBirth box (i disabled the other Proxy validators) I correctly get a "Date Of Birth is not in the correct format". The I change the Ruleset using the drop-down in the bottom to "RuleSetB" and enter "abcd" and I get a "Date Of Birth is not in the correct format"!

This is what I would expect!

But changing the RuleSet dropdown to RuleSetB should be equal to NOT having any ValidatorAttributes on the Property and as such I would expect the same behaviour.

But if I remove the RelativeDateTimeValidator from the property I get no error when I enter "abcd" using either RuleSet.

So I'll raise an issue on this.
Oct 20, 2007 at 3:34 AM

The property proxy validator really tries to apply the validation specified for a property to the value represented by the control; converting it is really a pre-req of the validation, so it's only applied if there is validations to apply.

Conversion errors are really in a gray area, but I agree it would be more consistent if the conversion was attempted even if there is no validation to apply. Logging as an issue.

Oct 20, 2007 at 3:35 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.