Date Validation - VAB + Asp.net

Topics: Validation Application Block
Oct 21, 2008 at 1:29 PM
Hello!

Now I have a new problem: How do I validate a date that is passed as a String in this format (ddmmyyyy)?

I tried the SelfValidation but I discovered here that this is not possible with asp.net integration (only if you use an instance of the class to do it).

I tried the TypeConversionValidator and I got closer to the solution but... It's necessary to use a cultural format like dd/mm/yyyy.

I tried the ValueConvert  and I guess that this is the way, but I can't figure out how to use both... the TypeConversion and the ValueConvert to transform a value like dd/mm/yyyy into ddmmyyyy, send it to the property and then validate it.

May anyone help me?

Regards!
Oct 21, 2008 at 2:43 PM
You can add [TypeConversionValidator(typeof(DateTime))] attribute to the property you want to convert.  Add a propertyproxyvalidator to your page and configure the SourceTypeName, PropertyName, and ControlToValidate properties.  No need to add any logic to the OnValueConvert event of the validator.
Oct 21, 2008 at 4:17 PM
Thank you for your response cshark,

But as I've said... I tried to use it... but in this case the property value must be a string in this format dd/mm/yyyy and what I need is ddmmyyyy but this is invalid for TypeConversion.
Besides that I forgot to mention that I have an aditional validation. The data must be greater than today.

I guess that it's not necessary to create a custom validator by the way that's what I'm trying to do right now... If anyone knows how to do it without a custom validator... Please, Post a reply.

Regards,
Roberto O Santos
www.robertoosantos.com
Oct 21, 2008 at 4:45 PM
In that case, add the logic for converting the ddmmyyyy to dd/mm/yyyy using the ValueToConvert property of the ValueConvert event's event args. After that, convert it to datetime format and check if it's greater than the date today. If this results to true, assign the dd/mm/yyyy to the ConvertedValue of the event args property.  Note, that that value is the string type, not the converted datetime value.  If false, assign any invalid string to e.ConvertedValue.  Enclose this in a try catch block to catch data type conversion errors. You can modify the e.ErrorMessage property as well.  You would still need to add the TypeConversionValidator attribute in the property. 

 

Oct 22, 2008 at 1:29 PM
Thank you for your help cshark,

I solved it using a custom validator.

Regards,
Roberto O Santos
www.robertoosantos.com