Skip to content

Instantly share code, notes, and snippets.

@khalidabuhakmeh
Created August 4, 2011 14:54
Show Gist options
  • Save khalidabuhakmeh/1125350 to your computer and use it in GitHub Desktop.
Save khalidabuhakmeh/1125350 to your computer and use it in GitHub Desktop.
Cascading Validation on children validators
public class OrderViewModel {
public bool UsingOldCard {get;set;}
public int OldCardId {get;set;}
public NewCardViewModel NewCard {get;set}
public int ItemId {get;set;}
}
public class NewCardViewModelValidator: AbstractValidator<NewCardViewModel> {
public class NewCardViewModelValidator() {
RuleFor(m => m.FirstName).NotEmpty();
// etc.
}
}
public class OrderViewModelValidator: AbstractValidator<OrderViewModel> {
public OrderViewModelValidator() {
RuleFor(m => NewCard).Unless(m => m.UsingOldCard); // doesn't exist (I don't think)
}
}
// Also would be cool to see
public class OrderViewModelValidator: AbstractValidator<OrderViewModel> {
public OrderViewModelValidator() {
GroupRules(m => {
m.OldCardId.NotEmpty();
m.ItemId.NotEmpty();
}).Unless(m => ProductNotSoldOut(m.ItemId)); // doesn't exist (I don't think), different from RuleSets. I only want to execute all these rules if a condition is met.
}
}
@JeremySkinner
Copy link

Like this?

RuleFor(x => x.NewCard).SetValidator(new NewCardViewModelValidator()).Unless(x => x.UsingOldCard)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment