19

我有一个 MVC3 视图模型定义为:

[Validator(typeof(AccountsValidator))]
public class AccountViewModel
{
    public List<string> Accounts { get; set; }
}

使用 FluentValidation (v3.3.1.0) 定义的验证为:

public class AccountsValidator : AbstractValidator<AccountViewModel>
{
    public AccountsValidator()
    {
        RuleFor(x => x.Accounts).SetCollectionValidator(new AccountValidator()); //This won't work
    }
}

并且可能会定义帐户验证:

public class AccountValidator : AbstractValidator<string> {
    public OrderValidator() {
        RuleFor(x => x).NotNull();
        //any other validation here
    }
}

我希望列表中的每个帐户都按照文档中的说明进行验证。但是,调用SetCollectionValidator不起作用,因为在使用 a 时这不是一个选项,List<string>尽管如果将选项定义为List<Account>. 我错过了什么吗?我可以更改要使用的模型List<Account>,然后定义一个 Account 类,但我真的不想更改模型以适应验证。

作为参考,这是我正在使用的视图:

@model MvcApplication9.Models.AccountViewModel

@using (Html.BeginForm())
{
    @*The first account number is a required field.*@
    <li>Account number* @Html.EditorFor(m => m.Accounts[0].Account) @Html.ValidationMessageFor(m => m.Accounts[0].Account)</li>

    for (int i = 1; i < Model.Accounts.Count; i++)
    {
        <li>Account number @Html.EditorFor(m => m.Accounts[i].Account) @Html.ValidationMessageFor(m => m.Accounts[i].Account)</li>
    }

    <input type="submit" value="Add more..." name="add"/>
    <input type="submit" value="Continue" name="next"/>
}
4

4 回答 4

21

以下应该有效:

public class AccountsValidator : AbstractValidator<AccountViewModel>
{
    public AccountsValidator()
    {
        RuleFor(x => x.Accounts).SetCollectionValidator(
            new AccountValidator("Accounts")
        );
    }
}

public class AccountValidator : AbstractValidator<string> 
{
    public AccountValidator(string collectionName)
    {
        RuleFor(x => x)
            .NotEmpty()
            .OverridePropertyName(collectionName);
    }
}
于 2012-04-17T11:52:06.143 回答
5

尝试使用:

public class AccountsValidator : AbstractValidator<AccountViewModel>
{
   public AccountsValidator()
   {
       RuleForEach(x => x.Accounts).NotNull()
   }
}
于 2018-04-12T15:28:26.010 回答
1

你可以使用RuleForEach There is an easy class with a list of string

   public class Request
    {
        public IEnumerable<string> UserIds { get; set; }      
        public string  Body { get; set; }        
    }

我创建了下一个验证器

public class RequestValidator : AbstractValidator<Request>
    {
        public RequestValidator()
        {        
            RuleForEach(x => x.UserIds).NotNull().NotEmpty();            
            RuleFor(x => x.Body).NotNull().NotEmpty();      
        }
    }
于 2019-10-17T09:15:41.800 回答
0

验证类:

using FluentValidation;
using System.Collections.Generic;

namespace Test.Validator
{

    public class EmailCollection
    {
        public IEnumerable<string> email { get; set; }

    }

    public class EmailValidator:  AbstractValidator<string>
    {
        public EmailValidator()
        {
            RuleFor(x => x).Length(0, 5);
        }

    }

    public class EmailListValidator: AbstractValidator<EmailCollection>
    {
        public EmailListValidator()
        {
            RuleFor(x => x.email).SetCollectionValidator(new EmailValidator());
        }

    }



}

注意:我使用了最新的 MVC 5 (Nuget) 版本的 fluentvalidation。

于 2017-04-27T17:20:42.007 回答