Key property visibility

Topics: Validation Application Block
Oct 22, 2007 at 7:39 PM
Let's suppose I needed to perform a string validation from within a SelfValidation decorated method because I can't use attribute-driven OR config-driven validation. The details of why this situation exists are not really important (although they are very good reasons), but in the end, I can only use SelfValidation. So, in my SelfValidation method(s), I want to leverage the existing EL validators to do the dirty work, rather than re-writing my own StringValidator, etc. My problem is that there's no way to pass the "Key" or "Tag" properties to the StringLengthValidator, and so I end up with ValidationResults that say useless things like "The length of the value must fall within the range "1" (Inclusive) - "50" (Inclusive)." If it can't say what value, then the message is not of much use to me. I could enforce that each instance like this must specify a complete validation message, but I'm still losing a lot of detail here.

Does anyone have any suggestions about how I can call built-in validators from code and provide the missing pieces? So far, the best I've got is iterating over the ValidationResults, and back-filling them individually, but that seems very hackish to me, and I'm not terribly satisfied with that answer.
Oct 22, 2007 at 8:39 PM
Hi Mel,

I see two issues you need to solve: feeding the values and the context information to the validators, and getting the context information in the self validation method.
For the first issue you might be able to use a PropertyValueValidator. However, it's likely you don't have a property where to get the value from, given your constraints.

My guess it that you will need to update the code and build your own binaries for this. Dealing with the second issue will prove harder, as you'll need to update the signature of the self validation methods (and the associated unit tests). If you don't need the extra information in the self validation method, this change in the code could be as small as changing the visibility for the DoValidate method.

Fernando