UI Validation with the MVP pattern. Part #03

As we discussed in the previous posts, MVP UI Validation 01 and MVP UI Validation 02, thanks to the Microsoft enterprise library, we can easily handle a perfect and professional UI Validation without leaving the MVP pattern in our Client applications.

Today I want to show you a final solution, that will handle in a professional way the UI validation.

The basic validation, refractor process.

In order to view what I am going to do today, you MUST read the previous articles related to the UI validation with the enterprise library. Actually we have our domain object that is in this way:


We need to do some refactoring, in order to use our validation process in a better way. First of all let’s create a new entity called ErrorResult that will contain some useful information and, of course, its custom collection.


Now let’s go back to the Domain Object entity and let’s do some refactoring. First of all we need to change the ListResults in this way:

Then we need to change the validation function. We still declare the validation results collection:

Then we verify that our custom collection is initialized:

And finally we loop the errors and we populate the custom collection in this way:

The Customer Entity and its validation attributes.

Now we can have fun and build a custom entity that will inherit from the domain object abstract class and that will implement some custom validations.

And we can do some simple test. The first test will verify that we respect the NotNull validation rule:

Note: I added a simple ToString() function in the custom error collection in order to have the entire output in one line, only for my tests …

Reflect the validation trough the Presentation layer.

Now we need to create a simple form and a custom presentation layer in order to apply the UI logic. First of all, the form. Remember to include and error component in your form:


Then this is the view:

Finally, this is the implementation of the view in our Windows Form:

I just added a couple of routines to better manage the control in my form: ClearErrors and GetControl.

Now it’s time for the presenter. This presenter will be very easy. It will have a private IView as a field and only one constructor:

I have created also a custom init routine in order to fill my form from the presenter:

And this is the routing that fill the view from the presenter:

Last is the SubmitChanges called by the view. Here is where we are running the domain validation logic:

So for each error raised by our Entity, I am going to add the error to the corresponding control in the view. If there are no errors at all, the view will be cleaned. In this way we can also enable/disable a save button for example. But it is not the purpose of this article.

The final result will be:


I hope you enjoy my series of articles, and if you have any feedback, feel free to post them on my blog.

The next series will focus on the MVVM, I have already posted something.

If you want the solution of this tutorial, it is saved in my sky drive here