【问题标题】:FluentValidation - Validating a View Model that contains a list of an ObjectFluentValidation - 验证包含对象列表的视图模型
【发布时间】:2014-01-23 13:36:56
【问题描述】:

我正在一个包含复杂视图模型的项目上尝试 FluentValidation,我阅读了 documentation here,但我不知道如何设置规则来验证在我的视图模型中声明的对象列表。在下面的示例中,视图模型中的列表包含 1 个或多个 Guitar 对象。谢谢

查看模型

 [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; } 
    }

View Model 中使用的吉他类

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

查看模型验证器类

 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????


        }
    }

【问题讨论】:

  • 你不能有单独的吉他类验证器吗?你想在 Ilist 上进行什么样的验证,是否有多个元素?

标签: c# asp.net-mvc-3 fluentvalidation


【解决方案1】:

此代码已弃用:RuleFor(x =&gt; x.Guitars).SetCollectionValidator(new GuitarValidator());

这是新的:

RuleForEach(x => x.Guitars).SetValidator(new GuitarValidator());

【讨论】:

  • 这很有趣
【解决方案2】:

您可以将此添加到您的 CustomerViewModelValidator

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

所以您的 CustomerViewModelValidator 看起来像这样:

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());
    }
}

添加 GuitarValidator 看起来像:

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

【讨论】:

  • SetCollectionValidator 已被弃用,现在使用 RuleForEach。
  • RuleForEach(x => x.Guitars).SetValidator(new GuitarValidator()) 的语法;参考答案stackoverflow.com/a/53227565/193061
【解决方案3】:
RuleForEach(
  itemToValidate => 
     new YourObjectValidator());


public class YourObjectValidator : AbstractValidator<YourObject>
{
  public EdgeAPIAddressValidator()
  {
     RuleFor(r => r.YourProperty)
         .MaximumLenght(100);
  }
}

【讨论】:

  • 虽然这段代码 sn-p 可能会回答这个问题,包括对为什么如何的解释,它有助于解决问题,提高质量和寿命你的答案,特别是关于像这样的老问题。见"How do I write a good answer?"
【解决方案4】:

它适用于最新版本的 Fluent 并包含要使用的完整示例。

答案中的代码已弃用。

【讨论】:

    【解决方案5】:

    你可以使用childrule

    RuleForEach(p => p.Guitars).ChildRules(child =>{
    child.RuleFor(x=>x.Make).NotEmpty().WithMessage({PropertyName}required.") .NotNull(); });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多