gmail user
gmail user

Reputation: 2783

Fluent Validation with ASP.NET MVC 5

We've MVC-2 application with Fluent Validation (Visual Studio 2010). Recently we migrated it to Visual Studio 2013, MVC 5. After upgrading the Fluent Validation with NuGet, I get the compilation error for following members.

Can't find namespace FluentValidation.Mvc.MetadataExtensions

I'm using UIHint, DataType, DisplayName members from FluentValidation.Mvc.MetadataExtensions.MetadataExtensions class. Which class should I use to fix errors?

EDIT

Broken code

[FluentValidation.Attributes.Validator(typeof(class1Validator))]
public partial class class1: BusinessBase<decimal>
{
}

public class class1Validator: FluentValidation.AbstractValidator<class1>
{
    public class1Validator()
    {
        RuleFor(o => o.FirstName).NotEmpty().Length(1, 60).UIHint("First Name"); //giving error for UIHint
        RuleFor(o => o.Dob).DataType(System.ComponentModel.DataAnnotations.DataType.Date).GreaterThan(DateTime.MinValue).LessThan(DateTime.Now); //giving error for datatype

    }
}

 public class SearchValidator : FluentValidation.AbstractValidator<SearchCriteria>
{
    public SearchValidator()
    {
        RuleFor(o => o.Id).DisplayName("User ID"); //giving error for DisplayName in both lines
        RuleFor(o => o.LastName).DisplayName("Last Name");           

    }


}

Upvotes: 0

Views: 1476

Answers (1)

Derek Van Cuyk
Derek Van Cuyk

Reputation: 953

It appears that these extension methods are deprecated. I created a web project to do some research.

In any recent versions (FluentValidation.Mvc3+), I cannot find the extension methods.

When I created a project with FluentValildation.Mvc2, they exist.

You will need to either use the older library (and I don't know how well they play with the newer versions of MVC) or use the corresponding data annotations for each of the deprecated extension methods.

public class BusinessBase<T>
{
   [UIHint("First Name")]
   public string FirstName { get; set; }
   public DateTime Dob { get; set; }
}

I hope this helps.

Upvotes: 1

Related Questions