Fluent验证不适用于复杂属性Asp.Net

时间:2012-04-04 07:35:51

标签: asp.net fluentvalidation

我是Fluent验证的新手,需要一些帮助。我有一个接口,有另一个接口属性,我已经为两个接口编写了验证。挑战在于它没有显示接口属性的验证消息。

下面是我的情况代码

public interface IAddress
{
    string City { get; set; }
    string Town { get; set; }
}

public interface IAccount
{
    string FullName { get; set; }
    int Age { get; set; }
    IAddress Address { get; set; }
}

    public AccountValidator ()
    {
        RuleFor(x => x.FullName).NotEmpty().WithMessage("Full Name  can not  be  empty");
        RuleFor(x => x.Age).GreaterThan(18).WithMessage("Age cannot  be  less  than 18 years");
        RuleFor(x => x.Address).SetValidator( new AddressValidator()); 
    }

    public AddressValidator()
    {
        RuleFor(x => x.City).NotEmpty().NotNull().WithMessage("City can not  be empty");
        RuleFor(x => x.Town).NotEmpty().NotNull().WithMessage("Town can not  be empty"); 
    }

我用来调用验证的客户端代码是:

        var accountValidator = new AccountValidator();

         accountValidator.ValidateAndThrow(_account );

提前致谢。

1 个答案:

答案 0 :(得分:0)

好吧,我尝试了一个迷你控制台应用程序,并且很好地捕获了异常(年龄+城市) (我juste用Not(...)更改了NotEmpty()。NotNull(),因为你需要为每个或它们放置一个特定的“WithMessage”。

我是否误解了你的问题?

public interface IAddress
    {
        string City { get; set; }
        string Town { get; set; }
    }

    public interface IAccount
    {
        string FullName { get; set; }
        int Age { get; set; }
        IAddress Address { get; set; }
    }

    public class Address : IAddress
    {
        public string City { get; set; }
        public string Town { get; set; }
    }
    public class Account : IAccount
    {
        public string FullName { get; set; }
        public int Age { get; set; }
        public IAddress Address { get; set; }
    }

    public class AccountValidator : AbstractValidator<IAccount>
    {
        public AccountValidator()
        {
            RuleFor(x => x.FullName).NotEmpty().WithMessage("Full Name  can not  be  empty");
            RuleFor(x => x.Age).GreaterThan(18).WithMessage("Age cannot  be  less  than 18 years");
            RuleFor(x => x.Address).SetValidator(new AddressValidator());
        }
    }

    public class AddressValidator : AbstractValidator<IAddress>
    {
        public AddressValidator()
        {
            RuleFor(x => x.City).Must(x => !string.IsNullOrEmpty(x)).WithMessage("City can not  be empty");
            RuleFor(x => x.Town).Must(x => !string.IsNullOrEmpty(x)).WithMessage("Town can not  be empty");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            var account = new Account
                               {
                                   Address = new Address
                                                 {
                                                     City = "",
                                                     Town = "Town"
                                                 },
                                   Age = 18,
                                   FullName = "FullName"
                               };
            var accountValidator = new AccountValidator();
            try
            {
                accountValidator.ValidateAndThrow(account);
            }
            catch (ValidationException e)
            {
                Console.WriteLine(e);
            }
            Console.ReadKey();

        }
    }