VAB, validating DateTime, Decimal etc values in entity?

Topics: Validation Application Block
Apr 11, 2007 at 5:54 PM
Hi there,
I am wondering how to use the VAB to validate that the user is inserting a valid DateTime or Decimal value when submitting a form. I have a property of type DateTime but there is no DateTimeValidator that I can see. I have tried a RegexValidator but I get back a message "The supplied value could not be converted to the target property type." where I would like to see a message like "Invalid date." if the user enters in "ABC" for a datetime. I am using the PropertyProxyValidator to show the validation message back to the user. Likewise validating against and other property types (IE decimals ect) Any help would be greatly appreciated!
Apr 11, 2007 at 8:20 PM
Hi Shawn -

There are a couple of ways of deailing with this. One important fact to understand is that the VAB validates against strongly-typed objects and properties, not against text boxes. So if you have a property of type DateTime and a text box for entering data for that property, the validation rules will expect to be given a DateTime value. If the supplied value is a valid date, the VAB can apply rules such as the RelativeDateTimeValidator to see if the data is valid, but if the value from the text box isn't a DateTime at all then the rules cannot be run.

Luckily we did consider this scenario in the Winforms and ASP.NET integration layers for the VAB. The PropertyProxyValidator and the Winforms Validation component both raise an event called ValueConvert. This event is fired during validation but before the VAB is called, and gives you the opportunity to convert user input to the expected type. If the value cannot be converted you can create a pseudo-validation error (I call it that since the error wasn't really generated by the block, but it will be displayed to users in the same way). Both of the VAB QuickStarts demonstrate the event.

The other thing the block provides for this problem is the TypeConversionValidator, which checks if a string can be converted to a specific type in the current culture. On the surface this does exactly what you are asking for, but the catch is that the rule must be run against a string, not against the target type. If you're not using the integration layers you could use this validator to programmatically validate data in the control before converting, I guess.

Hope this helps
Apr 12, 2007 at 8:19 PM
Thanks for the reply Tom!!
I have implamented something using the ValueConvert event, unfortunatly if I have 10 dates, I have to fire all 10 events??? This seems like a lot of work for just validating to make sure the user has entered in a proper date in the field. The TypeConversionValidator works except I get back a generic message "The supplied value could not be converted to the target property type." which I cannot change to a custom message using the message template.

I am starting to wonder if I am using the incorrectly??

Just to give you more information.... I have a webform with a textbox(s) to enter in a date, I want to validate it to make sure it is a value date instead of garbage text and issue a message back saying the inputed value was not a valid date. I am using a class with a datetime property that needs to get set/saved, this is where I am using my VAB validators (NotNullValidator etc).

If you have anything more to add I would be greatly appreciated. Thanks again!
Apr 12, 2007 at 9:52 PM
Hi Shawn -

With the ASP.NET VAB integration you will need to handle the event for each date control. The ASP.NET VAB QuickStart has a demonstration of exactly what you're asking for - there is one date field and one integer field that are both wired up to events and display a friendly pseudo-validation message when conversion fails.