Created
August 4, 2011 14:54
-
-
Save khalidabuhakmeh/1125350 to your computer and use it in GitHub Desktop.
Cascading Validation on children validators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} | |
} |
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
I don't want to run the child validation of NewCardViewModelValidator unless the condition is met. Does this make sense?