Skip to content

Commit

Permalink
Add second AddToModelState overload
Browse files Browse the repository at this point in the history
  • Loading branch information
RehanSaeed authored and JeremySkinner committed Sep 8, 2022
1 parent eafb047 commit 7f3ad0d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/FluentValidation.AspNetCore/ValidationResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,26 @@ public static class ValidationResultExtension {

private const string _rulesetKey = "_FV_ClientSideRuleSet";

/// <summary>
/// Stores the errors in a ValidationResult object to the specified modelstate dictionary.
/// </summary>
/// <param name="result">The validation result to store</param>
/// <param name="modelState">The ModelStateDictionary to store the errors in.</param>
public static void AddToModelState(this ValidationResult result, ModelStateDictionary modelState) {
if (!result.IsValid) {
foreach (var error in result.Errors) {
modelState.AddModelError(error.PropertyName, error.ErrorMessage);
}
}
}

/// <summary>
/// Stores the errors in a ValidationResult object to the specified modelstate dictionary.
/// </summary>
/// <param name="result">The validation result to store</param>
/// <param name="modelState">The ModelStateDictionary to store the errors in.</param>
/// <param name="prefix">An optional prefix. If omitted, the property names will be the keys. If specified, the prefix will be concatenated to the property name with a period. Eg "user.Name"</param>
public static void AddToModelState(this ValidationResult result, ModelStateDictionary modelState, string prefix = null) {
public static void AddToModelState(this ValidationResult result, ModelStateDictionary modelState, string prefix) {
if (!result.IsValid) {
foreach (var error in result.Errors) {
string key = string.IsNullOrEmpty(prefix)
Expand Down

0 comments on commit 7f3ad0d

Please sign in to comment.