Skip to content

Commit 7f3ad0d

Browse files
RehanSaeedJeremySkinner
authored andcommitted
Add second AddToModelState overload
1 parent eafb047 commit 7f3ad0d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/FluentValidation.AspNetCore/ValidationResultExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,26 @@ public static class ValidationResultExtension {
3131

3232
private const string _rulesetKey = "_FV_ClientSideRuleSet";
3333

34+
/// <summary>
35+
/// Stores the errors in a ValidationResult object to the specified modelstate dictionary.
36+
/// </summary>
37+
/// <param name="result">The validation result to store</param>
38+
/// <param name="modelState">The ModelStateDictionary to store the errors in.</param>
39+
public static void AddToModelState(this ValidationResult result, ModelStateDictionary modelState) {
40+
if (!result.IsValid) {
41+
foreach (var error in result.Errors) {
42+
modelState.AddModelError(error.PropertyName, error.ErrorMessage);
43+
}
44+
}
45+
}
46+
3447
/// <summary>
3548
/// Stores the errors in a ValidationResult object to the specified modelstate dictionary.
3649
/// </summary>
3750
/// <param name="result">The validation result to store</param>
3851
/// <param name="modelState">The ModelStateDictionary to store the errors in.</param>
3952
/// <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>
40-
public static void AddToModelState(this ValidationResult result, ModelStateDictionary modelState, string prefix = null) {
53+
public static void AddToModelState(this ValidationResult result, ModelStateDictionary modelState, string prefix) {
4154
if (!result.IsValid) {
4255
foreach (var error in result.Errors) {
4356
string key = string.IsNullOrEmpty(prefix)

0 commit comments

Comments
 (0)