你如何验证对使用流利的验证列表中的每个字符串? [英] How do you validate against each string in a list using Fluent Validation?

查看:72
本文介绍了你如何验证对使用流利的验证列表中的每个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  [验证(typeof运算(AccountsValidator))]
公共类AccountViewModel
{
    公开名单<串GT;帐户{搞定;组; }
}

通过验证使用FluentValidation(v3.3.1.0)的定义是:

 公共类AccountsValidator:AbstractValidator< AccountViewModel>
{
    公共AccountsValidator()
    {
        RuleFor(X => x.Accounts).SetCollectionValidator(新AccountValidator()); //这将无法正常工作
    }
}

和帐户验证可能将被定义为:

 公共类AccountValidator:AbstractValidator<串GT; {
    公共OrderValidator(){
        RuleFor(X =&X的催化剂).NotNull();
        //这里的任何其他验证
    }
}

我想在列表中的每个帐户在<一的描述进行valdiated href=\"http://fluentvalidation.$c$cplex.com/wikipage?title=CreatingAValidator&referringTitle=Documentation&ANCHOR#Collections\">documentation.然而,在调用 SetCollectionValidator 不起作用,因为这使用时,是不是一种选择,一个列表&LT;串&GT; 虽然该选项会在那里,如果它被定义为列表&LT;&帐户GT; 。我缺少的东西吗?我可以改变我的模型中使用列表&LT;账户方式&gt; ,然后定义一个账户类,但我真的不希望改变我的模型,以适应审定

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

  @model MvcApplication9.Models.AccountViewModel@using(Html.BeginForm())
{
    @ *第一个帐号是必填字段。* @
    &LT;立gt;帐户数量* @ Html.EditorFor(M = GT; m.Accounts [0] .Account)@ Html.ValidationMessageFor(M = GT; m.Accounts [0] .Account)LT; /李&GT;    的for(int i = 1; I&LT; Model.Accounts.Count;我++)
    {
        &LT;立gt;帐户号码@ Html.EditorFor(M = GT; m.Accounts [I] .Account)@ Html.ValidationMessageFor(M = GT; m.Accounts [I] .Account)LT; /李&GT;
    }    &LT;输入类型=提交值=添加更多...NAME =添加/&GT;
    &LT;输入类型=提交值=继续NAME =下一个/&GT;
}


解决方案

下面应该工作:

 公共类AccountsValidator:AbstractValidator&LT; AccountViewModel&GT;
{
    公共AccountsValidator()
    {
        RuleFor(X =&GT; x.Accounts).SetCollectionValidator(
            新AccountValidator(帐户)
        );
    }
}公共类AccountValidator:AbstractValidator&LT;串GT;
{
    公共AccountValidator(字符串集合名)
    {
        RuleFor(X =&GT; x)的
            。不是空的()
            .OverridePropertyName(集合名);
    }
}

I have an MVC3 view model defined as:

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

With the validation defined using FluentValidation (v3.3.1.0) as:

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

And the account validation would possibly be defined:

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

I would like each account in the list to be valdiated as described in the documentation. However, the call to SetCollectionValidator doesn't work as this is not an option when using a List<string> although the option would be there if it were defined as List<Account>. Am I missing something? I could change my model to use List<Account> and then define an Account class but I don't really want to change my model to suit the validation.

For reference, this is the view that I am using:

@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"/>
}

解决方案

The following should work:

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

这篇关于你如何验证对使用流利的验证列表中的每个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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