Showing posts with label Validation. Show all posts
Showing posts with label Validation. Show all posts

Extending EntLib VAB - Part 2.2: Enable unobtrusive jQuery validation for Custom Validator

3 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.

… Continuation from Extending EntLib VAB - Part 2.1: Create custom validators and integrate that with EntLib CABC.

Now lets see how we can enable client side unobtrusive validation for this custom validator by creating adapter for the same and adding the adapter in our custom implementation of DataAnnotationsModelValidatorProvider  (explained in Extending EntLib VAB - Part 1: Enable client side support EntLib VAB and align the validation error keys with MVC ModelState keys) and finally writing unobtrusive jQuery validation.

Extending EntLib VAB - Part 2.1: Create custom validators and integrate that with EntLib CABC

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.

Here is the second last installment post of this series. If you are interested in checking the earlier posts here are the links: Extending Enterprise Library (EntLib) Validation Application Block (VAB) – Introduction, Extending EntLib VAB - Part 1: Enable client side support EntLib VAB and align the validation error keys with MVC ModelState keys (I strongly suggest to check at least part 1 of this series as we are going to reuse here a component that we have developed in that post).

In this post we will see how to develop a custom validator for EntLib VAB. If you are developing a custom validator, there are several aspects you have to take care for: Obviously the first and foremost is write your actual validator that works on server side and integrate that with CABC, next is to enable client side validation for your validator (now when we are talking about client side validation, in ASP.NET MVC it is important to write the client side validation in jQuery unobtrusive validation to provide your end user similar kind of experience as they have with other Out-Of-The-Box validators) and lastly as you are working with EntLib provide support so that your custom validator also seamlessly integrates with EntLib Configuration Application Block Console (CABC). In this post we are going to cover all of these.

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.

Customizing Validation Attributes in MVC

2 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.

Background

As many of you already know, MVC provides validation attribute functionality to validate view models. A few very commonly used validation attributes are: Required, RegularExpression and Range. Below is one simple example:


public class Person
    {
        [Required(ErrorMessage = "Please Enter your name")]
        public string Name { get; set; }
 
        [Required(ErrorMessage = "Please Enter your Email")]
        [RegularExpression(".+\\@.+\\‥+", ErrorMessage = "Please Enter valid Email")]
        public string Email { get; set; }
 
        [Required(ErrorMessage = "Please Enter your Phone")]
        public string Phone { get; set; }
    }


Which will give you the following output:
This default behavior is good for less complex application, where you can apply factory made validation without any considerable effort from your side.

You can also enable client side validation(validation will be done at client side without server post back) by adding/modifying the following lines in the
1. Web.config file:
<configuration>
  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
</configuration>

2. Layout / View page (of course you can use the latest version of jQuery):
    <script src="~/Scripts/jquery-1.9.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>


So far so good, Now lets complicate it

Lets assume that this application is going to be used by people from different regions and every region has a specific format for phone number. Every region has a separate set of special characters to be allowed for the phone number.