From 7f3ad0d29f9509637b643de31de57181edfd3e76 Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Saeed Date: Wed, 17 Aug 2022 13:46:03 +0100 Subject: [PATCH] Add second AddToModelState overload --- .../ValidationResultExtensions.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/FluentValidation.AspNetCore/ValidationResultExtensions.cs b/src/FluentValidation.AspNetCore/ValidationResultExtensions.cs index 698cfaa..2108aa3 100644 --- a/src/FluentValidation.AspNetCore/ValidationResultExtensions.cs +++ b/src/FluentValidation.AspNetCore/ValidationResultExtensions.cs @@ -31,13 +31,26 @@ public static class ValidationResultExtension { private const string _rulesetKey = "_FV_ClientSideRuleSet"; + /// + /// Stores the errors in a ValidationResult object to the specified modelstate dictionary. + /// + /// The validation result to store + /// The ModelStateDictionary to store the errors in. + public static void AddToModelState(this ValidationResult result, ModelStateDictionary modelState) { + if (!result.IsValid) { + foreach (var error in result.Errors) { + modelState.AddModelError(error.PropertyName, error.ErrorMessage); + } + } + } + /// /// Stores the errors in a ValidationResult object to the specified modelstate dictionary. /// /// The validation result to store /// The ModelStateDictionary to store the errors in. /// 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" - 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)