FluentValidation - 验证一个包含对象列表的视图模型 [英] FluentValidation - Validating a View Model that contains a list of an Object

查看:830
本文介绍了FluentValidation - 验证一个包含对象列表的视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在包含复杂的视图模型项目尝试FluentValidation和我读<一个href=\"https://fluentvalidation.$c$cplex.com/wikipage?title=CreatingAValidator&referringTitle=Documentation&ANCHOR#ReusingValidators\"相对=nofollow>文档这里,但我看不出如何设置规则来验证我的视图模型声明的对象的列表。在下面我的例子,在视图模型的列表中包含1个或多个吉他对象。谢谢

视图模型

  [FluentValidation.Attributes.Validator(typeof运算(CustomerViewModelValidator))]
    公共类CustomerViewModel
    {
        [显示(名称=名)]
        公共字符串名字{获得;组; }        [显示(名称=姓氏)]
        公共字符串名字{获得;组; }        [显示(NAME =手机)]
        公共字符串电话{搞定;组; }        [显示(名字=电子邮件)]
        公共字符串EmailAddress的{搞定;组; }        公开名单&LT;吉他与GT;吉他{搞定;组; }
    }

在视图模型中使用

吉他类

 公共类吉他
{
    公共字符串制作{搞定;组; }
    公共字符串模式{搞定;组; }
    公众诠释? ProductionYear {搞定;组; }
}

视图模型验证程序类

 公共类CustomerViewModelValidator:AbstractValidator&LT; CustomerViewModel&GT;
    {
        公共CustomerViewModelValidator()
        {
            RuleFor(X =&GT; x.FirstName).NotNull();
            RuleFor(X =&GT; x.LastName).NotNull();
            RuleFor(X =&GT; x.Phone).NotNull();
            RuleFor(X =&GT; x.EmailAddress).NotNull();
           //预计公司吉他这里的索引列表????
        }
    }


解决方案

您将添加到您的CustomerViewModelValidator

  RuleFor(X =&GT; x.Guitars).SetCollectionValidator(新GuitarValidator());

所以,你CustomerViewModelValidator是这样的:

 公共类CustomerViewModelValidator:AbstractValidator&LT; CustomerViewModel&GT;
{
    公共CustomerViewModelValidator()
    {
        RuleFor(X =&GT; x.FirstName).NotNull();
        RuleFor(X =&GT; x.LastName).NotNull();
        RuleFor(X =&GT; x.Phone).NotNull();
        RuleFor(X =&GT; x.EmailAddress).NotNull();
        RuleFor(X =&GT; x.Guitars).SetCollectionValidator(新GuitarValidator());
    }
}

添加GuitarValidator看起来是这样的:

 公共类GuitarValidator:AbstractValidator&LT;吉他与GT;
{
    公共GuitarValidator()
    {
        //吉他您的所有其他的验证规则。例如。
        RuleFor(X =&GT; x.Make).NotNull();
    }
 }

I am trying out FluentValidation on a project that contains complex view models and I read the documentation here but I don't see how to set up the rules to validate a list of objects declared in my view model. In my example below, the list in the view model contains 1 or more Guitar objects. Thanks

View Model

 [FluentValidation.Attributes.Validator(typeof(CustomerViewModelValidator))]
    public class CustomerViewModel
    {
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Display(Name = "Phone")]
        public string Phone { get; set; }

        [Display(Name = "Email")]
        public string EmailAddress { get; set; }

        public List<Guitar> Guitars { get; set; } 
    }

Guitar class used in View Model

public class Guitar
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int? ProductionYear { get; set; }
}

View Model Validator Class

 public class CustomerViewModelValidator : AbstractValidator<CustomerViewModel>
    {


        public CustomerViewModelValidator()
        {
            RuleFor(x => x.FirstName).NotNull();
            RuleFor(x => x.LastName).NotNull();
            RuleFor(x => x.Phone).NotNull();
            RuleFor(x => x.EmailAddress).NotNull();
           //Expects an indexed list of Guitars here????


        }
    }

解决方案

You would add this to your CustomerViewModelValidator

RuleFor(x => x.Guitars).SetCollectionValidator(new GuitarValidator());

So your CustomerViewModelValidator would look like this:

public class CustomerViewModelValidator : AbstractValidator<CustomerViewModel>
{
    public CustomerViewModelValidator()
    {
        RuleFor(x => x.FirstName).NotNull();
        RuleFor(x => x.LastName).NotNull();
        RuleFor(x => x.Phone).NotNull();
        RuleFor(x => x.EmailAddress).NotNull();
        RuleFor(x => x.Guitars).SetCollectionValidator(new GuitarValidator());
    }
}

Add the GuitarValidator would look something like:

public class GuitarValidator : AbstractValidator<Guitar>
{
    public GuitarValidator()
    {
        // All your other validation rules for Guitar. eg.
        RuleFor(x => x.Make).NotNull();
    }
 }

这篇关于FluentValidation - 验证一个包含对象列表的视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆