9

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.

2 Answers 2

24

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Dont use the FluentVallidation namespace directly, it should be implemented from servicestack. So you can write it as

using Servicestack.FluentValidation;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.