Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem getting started with FluentValidation

I am attempting to use FluentValidation 2.0 with an MVC 3 project. I have followed the instructions here to install FV within the project.

This is my validator class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentValidation;

namespace HandicapTracker.Models.Validation
{
    public class TournamentValidator : AbstractValidator<Tournament>
    {
        public TournamentValidator()
        {
            RuleFor(x => x.CourseId).NotEmpty();
            RuleFor(x => x.TournamentDate).NotEmpty().NotEqual(new DateTime());
        }
    }
}

Here is where I attempt to use the attribute:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using HandicapTracker.Configuration;
using HandicapTracker.Models.Validation;
using Omu.ValueInjecter;
using FluentValidation;
using HandicapTracker.Models.Validation;

namespace HandicapTracker.Models
{
    [Validator(typeof(TournamentValidator))]
    public class Tournament : HandicapTrackerModelBase
    {
        private const int VisitingTeamIndex = 0;
        private const int HomeTeamIndex = 1;

        public Tournament() : this (new League())
        {

        }
        ....
}

However, the attribute is not being recognized. When I build I get the following error message:

"System.ComponentModel.DataAnnotations.Validator" is not an attribute class.

I have actually tried this on two different solutions and am having the same problem on both. It's probably something trivial, but I can't figure it out.

Can someone tell me what i am doing wrong?

Thanks.

like image 645
Mike Moore Avatar asked Mar 17 '11 22:03

Mike Moore


1 Answers

It looks like your [Validator] attribute is picking up on another class called Validator in the System.ComponentModel.DataAnnotations namespace. Try fully qualifying the attribute.

[FluentValidation.Attributes.Validator(typeof(TournamentValidator))]

Otherwise, revise your using statements to avoid the Validator name collision.

like image 76
Adam Price Avatar answered Oct 16 '22 17:10

Adam Price