Validation: PropertyProxyValidator type cast errors when integer field is blank and range is required.

Topics: Validation Application Block
Jul 22, 2010 at 2:28 PM
I have a business object with an integer property. The property has a range rule of 1-100 I am using the PropertyProxyValidator to provide error message display in ASP.NET When the user leaves the asp.net field blank, I get a typecast exception from the validator control, rather than a message saying it isn't in the required range. I realize that I could use the valueconvert event to change the value to a zero but this creates a whole lot of extra programming (across the application) that shouldn't be necessary. Second, if the validation were changed to 0-100, I still want to force the user to enter the data, rather than having a net effect of the system defaulting the value. Is there a way to allow blank values to be validated without throwing type cast exceptions?
Jul 23, 2010 at 9:42 AM

For me utilizing the ValueConvert event is still the more efficient approach. And actually this does not require too much code effort and the ValueConvert method is all that it needs. You can check this blog for a sample implementation.  One other approach I can think of is by creating your own Custom Validator, this will give you more freedom on what you want to do during validation but this will definitely require more code effort compared to the ValueConvert approach. 

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jul 23, 2010 at 1:23 PM

Really, writing the same type of code over and over again is something you consider to be efficient?

I disagree.   This is an oversight in the design of the range validator and similar validators.   If I say a range should be between 1 and 100 and the value is blank, then the value is not in the range defined and it is a validation error.   An exception should not be thrown.

Jul 23, 2010 at 1:51 PM

I was under the impression that what are you trying to solve is this "Is there a way to allow blank values to be validated without throwing type cast exceptions?". I agree that it may be an oversight in the design but what I'm just suggesting is something that may somehow cover the hole. Anyway, what I have just said is something that I thought could help. :)

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 23, 2010 at 2:54 PM

Sorry if my response seemed harsh.  I do appreciate the assistance.   Given that my initial post stated that I was looking for something more efficient than using the value convert event, your response led me to believe that you were trying to convince me that value convert was a valid permanent approach for managing range validation.    I see now that you acknowledge there may be a gap in the design and that you were trying to help by providing a work around.   I do appreciate that you responded promptly to my original question.