Sometimes you just need to get a summary of all warnings and errors of an object. All validation is gathered in the IValidationContext and available on that class. However, there are some convenience classes that allow a developer to create a summary based on a specific tag. This convenience class is IValidationSummary, which gathers the right information from an instance of IValidationContext.
To retrieve a summary of all validations from a IValidationContext, use the following code:
var validationSummary = validationContext.GetValidationSummary(); |
To retrieve a summary of all validations with a specific tag from a IValidationContext, use the following code:
var validationSummary = validationContext.GetValidationSummary("tag"); |
The ValidationToViewModel attribute allows a developer to gather a summary of an object easily. For example, to get all the person related validation into the PersonValidationSummary property, use the property definition below:
[ValidationToViewModel(Tag = "PersonValidation")] public IValidationSummary PersonValidationSummary { get; set; } |
All validation results that have the tag PersonValidation will automatically be gathered into the PersonValidationSummary property after each validation sequence.