Checking Nested Models

I currently have a ViewModel setting:

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

... in this case, “RequiredIfOtherPropertyIsTrue” is a validator that does pretty much what it says: checks if another property is true (in this case our boolean value indicates whether a deposit is required) and BankInfo is another model which 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; } } 

Now, in our ViewModel, we have a flag associated with our Boolean value DepositRequired, and an editor for a special template for BankInfo. After the submission, we are unable to figure out how to disable validation in BankInfo if it is not required by the model (for example, even if we do not require a property in ViewModel, it still runs val on BankInfo and therefore fails on any form message). Is there a standard way to check w / nested model when binding ViewModel?

+5
source share
1 answer

Unfortunately, with built-in verification you will need to use

 ModelState.Remove("BankInfo"); 

to conditionally ignore any validation errors on this object.

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

and then let BankInfoValidator handle the validation of this object.

Sort of:

 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); } 
+5
source

Source: https://habr.com/ru/post/980313/


All Articles