I have a situation where I need to validate a child, but only if it exists. Basically the user can enter either a bank account or a credit card, and I only want to validate the one they enter.
Here are the Models:
public class AccountViewModel
{
[Required]
public bool isBankAccount { get; set; }
[RequiredIf("isBankAccount")]
public BankAccount BankAccount { get; set; }
[RequiredIf("isBankAccount",
IfNot = true)]
public CreditCard CreditCard { get; set; }
}
public class CreditCard
{
[Required]
[CreditCard]
public string CreditCardNumber { get; set; }
[Required]
[Range(1, 12)]
public int? ExpiryMonth { get; set; }
[Required]
[Range(2000, 3000)]
public int? ExpiryYear { get; set; }
[Required]
public string CardHolderName { get; set; }
}
public class BankAccount
{
[Required]
public string BSB { get; set; }
[Required]
[StringLength(10,
MinimumLength = 3)]
[NumbersOnly]
public string AccountNumber { get; set; }
[Required]
public string AccountHolderName { get; set; }
}
My problem is that the children's attributes are still being validated despite the parent attribute validating as true. Is there a way to stop the children from validating if the parent says so?