Simple MVC Application using Enterprise Library Validation

This is an example of a simple MVC application using Enterprise Library validation

First add the EL validation to your project using nuget:
nugetELValidation

Now the Enterprise Library Validation has to be added to the ModelValidatorProviders
Create a EnterpriseLibraryValidatorWrapper class

using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.Validation;

public class EnterpriseLibraryValidatorWrapper : ModelValidator
{
    private Validator _validator;

    public EnterpriseLibraryValidatorWrapper(ModelMetadata metadata, ControllerContext context, Validator validator)
        : base(metadata, context)
    {
        _validator = validator;
    }

    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        return ConvertResults(_validator.Validate(Metadata.Model));
    }

    private IEnumerable<ModelValidationResult> ConvertResults(IEnumerable<ValidationResult> validationResults)
    {
        if (validationResults != null)
        {
            foreach (ValidationResult validationResult in validationResults)
            {
                if (validationResult.NestedValidationResults != null)
                {
                    foreach (ModelValidationResult result in ConvertResults(validationResult.NestedValidationResults))
                    {
                        yield return result;
                    }
                }

                yield return new ModelValidationResult
                    {
                        Message = validationResult.Message,
                        MemberName = validationResult.Key
                    };
            }
        }
    }
}

And now create a EnterpriseLibraryValidatorProvider

using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.Validation;
  
public class EnterpriseLibraryValidatorProvider : ModelValidatorProvider {
  public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context) {
    Validator validator = ValidationFactory.CreateValidator(metadata.ModelType);
  
    if (validator != null) {
      yield return new EnterpriseLibraryValidatorWrapper(metadata, context, validator);
    }
  }
}

Now add it to the Application_Start() method in the global.asax

ModelValidatorProviders.Providers.Add(new EnterpriseLibraryValidatorProvider());

The Enterprise Library validation can be used now. Here’s a example:

using System;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace MvcEL6Validation.Models
{
    public class HomeDataModelEnterpriseLibrary
    {

        [RangeValidator(10, RangeBoundaryType.Inclusive, 110, RangeBoundaryType.Inclusive)]

        public int ValInt1 { get; set; }

        public int? ValInt2 { get; set; }

        [ContainsCharactersValidator("hello", ContainsCharacters.All)]
        [ContainsCharactersValidator("world", ContainsCharacters.All)]
        public string ValString1 { get; set; }

        [StringLengthValidator(5, 20)]
        public string ValString2 { get; set; }

        [NotNullValidator]
        public double ValDouble1 { get; set; }

        public double? ValDouble2 { get; set; }

        [RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, MessageTemplate = "Must be 18 years or older.")]
        public DateTime ValDateTime1 { get; set; }

        [RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, MessageTemplate = "Must be 18 years or older.")]
        public DateTime? ValDateTime2 { get; set; }
    }
}

Client side Validation should be disabled as no client validation is provided.

HtmlHelper.ClientValidationEnabled = false;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = false;

And here’s the validation faults…
ELValidationResults

code: https://github.com/damienbod/MvcEL6Validation.git

2 comments

  1. I am using EntLib 5 && MVC3. Your code compile but isn’t working. Is mandatory Entñlib v6.0?
    Thanks

  2. Hi Gabriel

    Could be EL5 to EL6 have some big changes, but I don’t know the changes in the Validation module or the dependencies.

    greetings Damien

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: