Custom Validatable class

Topics: Developer Forum
Coordinator
Dec 30, 2006 at 4:18 AM
Dans Question
"This is really cool stuff. I want to create a business object class that can be data bound against (by a third party). Unfortunately when I derive from the Base Class I get two extra public properties which are then exposed and shown on a grid view. This would not be a problem for me except that the person doing the data binding with a control doesn't know about the properties of my class (it will ultimately be autogenerated by the codedom). More information on the ValidationManager would be really helpful, as would a code sample on this. Keep up the good work. Dan"

Response
Dan. I have created a sample for you on how to do this. See "SamplesLibrary\PropertyValidationManager\CustomClassUsingPropertyValidationManager.cs"
Coordinator
Dec 30, 2006 at 4:24 AM
Download a copy of the latest release (http://www.codeplex.com/ValidationFramework/Release/ProjectReleases.aspx) or the latest source (http://www.codeplex.com/ValidationFramework/SourceControl/ListDownloadableCommits.aspx) for this file.
Jan 16, 2007 at 8:35 AM
Thanks - that works really nicely. Meaningful exceptions get raised based on the validation criteria. The only problem i have now is attempting to hide the public properties required for validation from data-binding - it looks like there is nothing that you can do for runtime data-binding to hide properties within a class.

Also, client side validation controls don't get auto generated - is there anything extra that needs to be done for this to happen?
Coordinator
Jan 16, 2007 at 9:52 AM
Edited Apr 8, 2007 at 10:10 AM
Dan

I am not sure what problems you are having with databinding and client side controls. Could you please contact me via my profile page.
http://www.codeplex.com/UserAccount/UserProfile.aspx?UserName=SimonCropp
Jan 17, 2007 at 3:46 PM
Thanks Simon, I have sent you an e-mail with a sample vs2005 web project zipped and attached.

Cheers,

Dan
Coordinator
Jan 30, 2007 at 10:38 AM
Result from email conversation

1. Hiding the properties from databinding.
You have a few options here
a. You could make those properties and methods internal. I figure that, based on what you have told me, this is not an option for you. So moving on
b. Do not have those methods and properties on each object. You could instead do the validation outside the object. Have a look at BusinessObject.InsertRecord2. The PropertyValidationManager does not actually need to be inside your object. It only needs the object to have validation attributes on its properties.
c. Create your own interface that is explicitly implemented.
These should now not show up in the databinding.

2. Defining custom error messages.
Each validation attribute has and "ErrorMessage" property. You assign it with the following syntax "RequiredStringValidator(ErrorMessage = "Please put in a first name.")".

3. Generating validation controls automatically when databinding
To be honest I have not worked out how to do this automagically yet for webforms. I have been mostly working with windows forms recently so it has not been high on my list of priorities. The workaround is to use the ValidatorGenerator in the Page_Load. You need to programmatically bind each control to a property. You will also need to get the latest version, that i just checked, because I made it a little easier for people in your scenario to do the validation.
See the Page_Load in TraditionalForm.cs of the SampleSite
Aug 15, 2007 at 1:16 AM
I realize this is an old conversation but nonetheless feel obliged to point out that you can in fact hide these properties, however, to do so requires a custom collection class which implements ITypedList (and a slew of other interfaces if you wish to fully support data binding, property change notifications, etc.).

For a sample implementation look at this ListBase<T> class (it references other source files, which you can find by browsing the folder structure):
http://www.mertner.com/svn/gentle/gentle/SourceBranches/2.0.0/Gentle.Common/Collections/
Coordinator
Aug 15, 2007 at 1:37 AM
But in this question we dont have a list. We have a single instance of a class.