Extending EntLib VAB - Part 1: Enable client side support EntLib VAB and align the validation error keys with MVC ModelState keys

13 comments


Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

As discussed in my last post EntLib VAB doesn't support client side validation (in js/jQuery) out-of-the-box. And also while validating object graph, EntLib VAB generate Validation Result keys which are different from the ModelState keys in MVC.

In this post we will discuss on this problem.

Solution to this problem is simple. We just need to create an adapter for EntLib VAB validator to MVC ValidationAttribute, i.e. a piece of code is needed that will read the EntLib Validation configuration xml and for each validator it will return the MVC counterpart of the validator, i.e. ValidationAttribute.

As for aligning the validation error keys with MVC ModelState keys, we need to leverage the default model validation of ASP.NET MVC.

And the great news is that one single piece of code addresses combines these two solutions.

Extending Enterprise Library (EntLib) Validation Application Block (VAB) - Introduction

0 comments


Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

Why EntLib VAB?

Those who have worked with ASP.NET MVC know how easily one can implement the model validation using Data Annotation. On top of that, jQuery unobtrusive validation support makes it more enjoyable.

However, for a complex requirement you may want to change the validation specification based on some condition. For example, let us assume that you are working on a multi-tenant application where for same UI screen each tenant requires a different set of validation specifications. Say the end users in U.S. expecting date field to be in ‘mm/dd/yyyy’ format, but for same field end users in India expecting ‘dd/mm/yyyy’ format. Now this is really a simple change of regular expression for the date property in the model. However as the Validation Data Annotations are actually attributes, you cannot change the actual regular expression at run time.

Therefore, the most simplistic solution could be to make different models per tenant having just a different set of validation specification. Nevertheless, if you do that, the application will not be multi-tenant in its true sense, not to mention the other associated downside of it like maintaining a large library of models per tenant, making different views, controllers, areas and so on.

Next thing you can do is to make complex Custom Validation Attribute as shown here just to make the validation specifications dynamic (or tenant specific per say).

So to mitigate the issue and to have a lot cleaner and manageable solution, Enterprise Library Validation Application Block can be utilized. Using EntLib Configuration Application Block Console (CABC), you can create tenant specific validation configuration on your Model and inject appropriate validation configuration at runtime. EntLib VAB comes with a set of useful validators like Not Null Validator (Required in MVC), Regular Expression Validator, String Length Validator, Property Compare Validator and so on. It also gives a facility to the developers so that they can design and develop their own validators and integrate the same with EntLib CABC.

So in a way it’s awesome… well almost.