Thursday, February 14, 2008

WPF Validation

I recently had to build out a validation solution for a WPF project I was working on. For those of you that have not worked with WPF (you are missing out) validation works differently. At first I was unsure about the way validation works in WPF and it seemed a little incomplete. I still believe it is incomplete, mainly because there is no built in way to do complete form validation unless you implement a custom solutions. This brings us to the point of this post. I recently wrote up an example of a few different ways to do complete form validation using WPF. I created a Validation manager that allows me to do this. The validation manager utilizes the creation of custom validation rules. You can download the whole paper and the example solution here. The paper includes validation approaches other developers have done as well.

The validation manager that is used in this approach allows you to handle validation at the form level and allows you to have your validation logic separated from your business objects. This is one thing the other approaches do not let you have. The validation manager is also not coupled at all with the DataContext of the page so it gives you more flexibility. The IDataError approach Paul Stovell uses can be helpful but is tightly coupled with the DataContext. There is also one other very nice feature the Validation Manager I created gives you. It gives you the ability to invalidate a field on the page programmatically. Even if all the custom validation rules that are bound to in pass you can still use the Validation manager to invalidate the field and put an error message on it. This gives you great extensibility to build out a validation framework and use the validation manager to handle invalidating whatever field needs to be invalidated.

No comments: