开发者

Problem getting started with FluentValidation

开发者 https://www.devze.com 2023-02-17 08:01 出处:网络
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.

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 Tourname开发者_JS百科nt() : 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.


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.


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

using Servicestack.FluentValidation;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号