验证嵌套模型 [英] Validating Nested Models

查看:85
本文介绍了验证嵌套模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前具有这样的ViewModel设置:

I currently have a ViewModel setup as such:

public class OurViewModel
    {
        public OurViewModel() { }

        [Required]
        public int LeadID { get; set; }
        [Required]
        public int Rate { get; set; }
        [Required]
        public bool DepositRequired { get; set; }
        [RequiredIfOtherPropertyIsTrue("DepositRequired")]
        public BankInfo { get; set; }
    }

...在这种情况下,"RequiredIfOtherPropertyIsTrue"是一个验证程序,其功能大致相同:检查另一个属性是否为真(在这种情况下,我们的布尔值指示是否需要存款),以及BankInfo是另一个看起来像这样的模型:

...in this case, "RequiredIfOtherPropertyIsTrue" is a validator that does pretty much what it says: checks to see if another property is true (in this case, our boolean indicating whether or not a deposit is required), and BankInfo is another model that looks something like this:

public class BankInfo
{
    public enum AccountTypeEnum
    {
        CHECKING,
        SAVINGS
    }

    public BankAccountInfo() { }

    [DisplayName("Account Number")]
    [Required(ErrorMessage = "You must provide a valid bank account number")]
    public String AccountNumber { get; set; }

    [DisplayName("Bank Routing Number")]
    [Required(ErrorMessage = "You must provide a valid routing number")]
    [StringLength(9, MinimumLength = 9, ErrorMessage = "Your bank routing number must be exactly 9 digits")]
    public String ABANumber { get; set; }

    [DisplayName("Bank Account Type")]
    [Required]
    public AccountTypeEnum AccountType { get; set; }

    [DisplayName("Name on Bank Account")]
    [Required(ErrorMessage = "You must provide the name on your bank account")]
    public String AccountName { get; set; }
}

现在,在我们的ViewModel中,我们有一个绑定到DepositRequired布尔值的复选框,以及一个带银行信息的自定义模板的EditorFor.提交后,我们很难弄清模型是否不需要在BankInfo IF 上禁用验证(例如,即使我们不需要ViewModel的属性,它仍然会触发val on BankInfo,因此,在任何表单发布中均惨遭失败).是否有任何标准方法可用于在ViewModel绑定上处理嵌套模型验证?

Now, in our ViewModel, we have a checkbox bound to our DepositRequired boolean, and an EditorFor w/ a custom template for BankInfo. Upon submission, we're having trouble figuring out how disable validation on BankInfo IF it's not required by the model (eg. even if we don't require the property on the ViewModel, it's still triggering val on BankInfo and therefore, failing miserably on any form post). Is there any standard way for dealing w/ nested model validation on ViewModel bind?

推荐答案

不幸的是,通过内置的验证,您必须使用

Unfortunately, with the built in validation, you'd have to use

ModelState.Remove("BankInfo");

有条件地忽略该对象上的任何验证失败.

to conditionally ignore any validation failures on that object.

如果可以选择使用FluentValidation,则可以在OurViewModelValidator中执行以下操作:

If using FluentValidation is an option, you can do something like this in your OurViewModelValidator:

RuleFor(ourViewModel=> ourViewModel.BankInfo).SetValidator(new BankInfoValidator()).When(ourViewModel=> ourViewModel.DepositRequired);

,然后让BankInfoValidator处理该对象的验证.

and then let the BankInfoValidator handle validation of that object.

类似:

public class BankInfoValidator : AbstractValidator<BankInfo>
{
    public BankAccountInfoValidator() 
    {
        RuleFor(bank => bank.AccountName).NotEmpty().WithMessage("You must provide a name for your bank account.");
        RuleFor(bank => bank.AccountNumber).NotEmpty().WithMessage("You must provide an account number for your bank information.");
        RuleFor(bank => bank.AccountType).NotEmpty().WithMessage("You must select what kind of bank account you're entering information for (checking, savings).");
        RuleFor(bank => bank.ABANumber).NotEmpty().WithMessage("You must provide a routing number for your bank information.");
        RuleFor(bank => bank.ABANumber).Must(BeOnlyDigits).WithMessage("Your routing number can only contain numeric characters, 0-9.");
        RuleFor(bank => bank.AccountNumber).Must(BeOnlyDigits).WithMessage("Your account number can only contain numeric characters, 0-9.");
    }

    private bool BeOnlyDigits(string digitString)
    {
        int result;
        return int.TryParse(digitString, out result);
    }

这篇关于验证嵌套模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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