Question Validating Properties in a class

tallg

New member
Joined
Dec 16, 2005
Messages
4
Programming Experience
10+
Is there a way to validate ALL properties in a class at once?

I have a class with various validations added to property "Getter" to validate user inputs. (The problem is that all my validations only invoke if a user input something to the field that bind to the property. Any fields that user simply bypass will not get validated)

Ideally I want to loop through each property and validate in a "Save()" method.

Is there a way to do this?

Thanks in advance
 
It doesn't actually make sense to validate the value of a property that never gets set inside the object itself. What you're talking about is a UI issue so the validation belongs in the UI. To that end, handle the Validating event of the control and validate for required data there. Before saving, you can call the ValidateChildren method of the form and it will cause that event to be raised on all controls.
 
It doesn't actually make sense to validate the value of a property that never gets set inside the object itself. What you're talking about is a UI issue so the validation belongs in the UI. To that end, handle the Validating event of the control and validate for required data there. Before saving, you can call the ValidateChildren method of the form and it will cause that event to be raised on all controls.

Thanks for your reply.

However, that's not quite true. I have number of forms using my class, and adding validation to each form, is duplicating and make harder for future amendments. Therefore I think its better to have all the validations in the class rather than in UI.

I have validations in most of the properties (in getter) to check null values, valid dates, range etc. etc.

When user click on the save button on the form, first I want to see if any errors prior call the save method. However, if a user did not input a value (say mandatory field) I cant catch that error as user never touch that field on the form (due to property getter never executed).

So I need to validate all properties and make sure there are no validation errors before Save the data.

Hope this make sense.
 
Back
Top