I am already using Enterprise Library 5 Validation for my model (which is used also in WCF methods), so I decided that I would like to avoid redundant validators from ASP.NET 4 MVC with DataAnnotations.
But it seems, Enterprise Library Validators are not picked up automatically by MVC.
if I use MVC DataAnnotations:
[RegularExpression(MyValidationExpressions.Email, ErrorMessage = MyValidationMessages.InvalidEmailMessage)]
public virtual string Email { get; set; }
HTML contains data-val-regex-pattern and the field is being validated client-side.
But if I use the existing EL based validation:
[RegexValidator(MyValidationExpressions.Email, MessageTemplate = MyValidationMessages.EmptyFieldMessage))]
public virtual string Email { get; set; }
it does not display validation errors client-side, and the generated HTML does not have any validation attributes.
What am I missing here, how do I force MVC to use the existing EL validators both client-side and server-side?
Solution:
I accepted the solution to move completely to DataAnnotations. It is the easiest way and it works fine with both EntLib 5 and MVC 4. But there is a little catch with ValidationFactory - I had to replace CreateValidatorFromAttributes with CreateValidator and specific flags. See this article for explanation about how DataAnnotations work with ValidationFactory:
CreateValidatorFromAttributes doesn't use DataAnnotations Attributes
Also DataAnnotations have the [Required] attribute which deals nicely with empty strings and strings which contain only spaces. There were some problems on VAB with that.