我有一个 AccountsViewModel 定义为:
[Validator(typeof(AccountsValidator))]
public class AccountsViewModel
{
public AccountsViewModel()
{
Accounts = new List<Account>();
Accounts.Add(new Account { AccountNumber = string.Empty }); //There must be at least one account
}
public List<Account> Accounts { get; set; }
}
我有以下流畅的验证:
public class AccountsValidator : AbstractValidator<AccountsViewModel>
{
public AccountsValidator()
{
//Validate that a single account number has been entered.
RuleFor(x => x.Accounts[0].AccountNumber)
.NotEmpty()
.WithMessage("Please enter an account number.")
.OverridePropertyName("Accounts[0].AccountNumber");
RuleFor(x => x.Accounts)
.SetCollectionValidator(new AccountValidator());
}
}
public class AccountValidator : AbstractValidator<Account>
{
public AccountValidator()
{
RuleFor(x => x.AccountNumber)
.Matches(@"^\d{9}a?[0-9X]$")
.WithMessage("Please enter a valid account number.");
//TODO: Validate that the account number entered is not a duplicate account
}
}
Accounts
如果帐号在集合中重复,我想添加一个错误。但是,在AccountValidator
课堂上我无权访问帐户集合(据我所知)。如何更改/重写此内容以访问帐户集合,以确保帐号不重复?