Simple TextBox WinForm Validation Example

May 23, 2007 at 10:00 PM
I'm trying to test out the WinForm integration of the VAB, but seem to be having difficulty getting things configured correctly. Because I wanted to keep things simple, I decided to just attempt validating whether a string is of length greater than 0. I created a configuration file with a VAB section, added a System.String type, added a rule set named "RuleSet", and added a String Length Validator under "Self". I was able to get this to work by manually calling the validator on a button click, so I tried to get it working with the WinForm integration classes.

I added and error provider and a validation provider named "validationProvider" to my form and have set the "Perform Validation on validationProvider" to true (not sure what to do about the "SourcePropertyName" setting. I then configured the validationProvider to use my errorProvider, and have set the SpecificationSource to Configuration. I've tried different things for the validationProvider's Source Type Name and the text box's fields "SourcePropertyName", but can't seem to figure out what exactly is expected here. What do I do from here? The example uses a custom business entity of Address and Customer, but I'm a bit unclear as to what is going on with that as yet.
May 24, 2007 at 3:09 PM
There is a ValidationQuickStart that comes with Enterprise Library that shows an example of Winform Integration.

The SourcePropertyName is the name of the property on the class to get validation rules. This is very important as this is where it gets the validation rules.

The ValidationQuickStart has all those setting populated so you can see what to add in your case.




David Hayden
Microsoft MVP C#
May 24, 2007 at 3:42 PM
I'm aware of the ValidationQuickStart as I indicated in my post, but the problem is that the example only demonstrates validation of custom types. I am attempting to validate a string directly as I've done from my code, so I don't want to create a custom type for the sole purpose of validating a base type such as a string, int, etc. I understand that the "SourcePropertyName" is intended to point to the property on the type being validated, but the fact that the ValidationProvider contains an attribute named "SpecificationSource" with the options of "Attributes", "Configuration", and "Both" seems to indicate that you can validate a type directly from configuration.

So, my question really is can you validate base types such as string, int, bool, etc. without needing to configure the ValidatonProvider attributes to point to a separate type to derive validation information (even when you wish to derive the validation information directly from configuration)?

May 26, 2007 at 5:32 PM
Can someone intimately familiar with the VAB explain how one might go about configuration the Winform controls to validate a string without using attributes? I haven't been able to get this to work since the Winform types all seem geared to derive the configuration information from a custom type specifying a property with the desired attributes. It seems as though the Winform types just simply don't expose the same flexibility that using the validation API directly does, but the thing that is confusing is the ability to set the "SpecificationSource" property to "Configuration" as if to indicate it should derive its validation information directly from configuration without the need for a custom type with attributes. If you are required to always specify some source type and property, what is the SpecificationSource=Configuration intended for? Thanks.

Feb 1, 2010 at 8:42 PM

Hello Derek,

Did anyone ever answer your question? I am running into the same issue now. Thanks for any help you can give.

Feb 2, 2010 at 3:48 AM

This is doable.  I did not configure a validator under the Self node, this note from the entlib hands on lab explains why:

"Integration with Windows Forms' validation mechanism utilizes the validation rules associated with a business object, but no instance of the validated type is actually used. Self-validation relies on methods on the validated type, so the validation rules implemented by such methods cannot be used to validate user input."

This is how I do it:

Added the type String in the configuration.  Added a rule set and choose the Length property and configured a RangeValidator for it.  Set the upper and lower bound and bound type.  I have a validation provider named stringValidationProvider, I set the SourceTypeName property to System.String, mscorlib.  For the textbox that will be validated, I set the ValidatedProperty on stringValidationProvider property to "TextLength" and "Length" for the SourcePropertyName on stringValidationProvider property.

Hope this helps.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.