/// <summary> /// Initializes a new instance of the <see cref="ValidationSummary"/> class and filters all the validations on the specified tag. /// </summary> /// <param name="validationContext">The validation context to base the summary on.</param> /// <param name="tag">The tag.</param> /// <exception cref="ArgumentNullException">The <paramref name="validationContext"/> is <c>null</c>.</exception> public ValidationSummary(IValidationContext validationContext, object tag) { Argument.IsNotNull("validationContext", validationContext); _fieldWarnings = validationContext.GetFieldWarnings(tag); _fieldErrors = validationContext.GetFieldErrors(tag); _businessRuleWarnings = validationContext.GetBusinessRuleWarnings(tag); _businessRuleErrors = validationContext.GetBusinessRuleErrors(tag); LastModified = validationContext.LastModified; LastModifiedTicks = validationContext.LastModifiedTicks; }
/// <summary> /// Gets the list messages. /// </summary> /// <param name="validationContext">The validation context.</param> /// <param name="validationResult">The validation result.</param> /// <returns> /// String representing the output of all items in the fields an business object. /// </returns> /// <remarks> /// This method is used to create a message string for field warnings or errors and business warnings /// or errors. Just pass the right dictionary and list to this method. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="validationContext"/> is <c>null</c>.</exception> public static string GetValidationsAsStringList(this IValidationContext validationContext, ValidationResultType validationResult) { Argument.IsNotNull("validationContext", validationContext); var messageBuilder = new StringBuilder(); switch (validationResult) { case ValidationResultType.Warning: foreach (var field in validationContext.GetFieldWarnings()) { messageBuilder.AppendLine("* {0}", field.Message); } foreach (var businessItem in validationContext.GetBusinessRuleWarnings()) { messageBuilder.AppendLine("* {0}", businessItem.Message); } break; case ValidationResultType.Error: foreach (var field in validationContext.GetFieldErrors()) { messageBuilder.AppendLine("* {0}", field.Message); } foreach (var businessItem in validationContext.GetBusinessRuleErrors()) { messageBuilder.AppendLine("* {0}", businessItem.Message); } break; default: throw new ArgumentOutOfRangeException(nameof(validationResult)); } return(messageBuilder.ToString()); }
/// <summary> /// Gets the list messages. /// </summary> /// <param name="validationContext">The validation context.</param> /// <param name="validationResult">The validation result.</param> /// <returns> /// String representing the output of all items in the fields an business object. /// </returns> /// <remarks> /// This method is used to create a message string for field warnings or errors and business warnings /// or errors. Just pass the right dictionary and list to this method. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="validationContext"/> is <c>null</c>.</exception> private static string GetListMessages(IValidationContext validationContext, ValidationResultType validationResult) { Argument.IsNotNull("validationContext", validationContext); var messageBuilder = new StringBuilder(); switch (validationResult) { case ValidationResultType.Warning: foreach (var field in validationContext.GetFieldWarnings()) { messageBuilder.AppendLine("* {0}", field.Message); } foreach (var businessItem in validationContext.GetBusinessRuleWarnings()) { messageBuilder.AppendLine("* {0}", businessItem.Message); } break; case ValidationResultType.Error: foreach (var field in validationContext.GetFieldErrors()) { messageBuilder.AppendLine("* {0}", field.Message); } foreach (var businessItem in validationContext.GetBusinessRuleErrors()) { messageBuilder.AppendLine("* {0}", businessItem.Message); } break; default: throw new ArgumentOutOfRangeException("validationResult"); } return messageBuilder.ToString(); }