Skip to content

Commit 3a5731c

Browse files
committed
refactor: 利用扩展方法重构代码
1 parent 2a15b88 commit 3a5731c

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/BootstrapBlazor/Validators/RequiredValidator.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public override void Validate(object? propertyValue, ValidationContext context,
5656
}
5757
if (propertyValue == null)
5858
{
59-
results.Add(CreateValidationResult(context));
59+
results.Add(GetValidationResult(context));
6060
}
6161
else if (propertyValue is string val)
6262
{
6363
if (!AllowEmptyString && val == string.Empty)
6464
{
65-
results.Add(CreateValidationResult(context));
65+
results.Add(GetValidationResult(context));
6666
}
6767
}
6868
else if (propertyValue is IEnumerable v)
@@ -71,27 +71,21 @@ public override void Validate(object? propertyValue, ValidationContext context,
7171
var valid = enumerator.MoveNext();
7272
if (!valid)
7373
{
74-
results.Add(CreateValidationResult(context));
74+
results.Add(GetValidationResult(context));
7575
}
7676
}
7777
else if (propertyValue is DateTimeRangeValue dv && dv is { NullStart: null, NullEnd: null })
7878
{
79-
results.Add(CreateValidationResult(context));
79+
results.Add(GetValidationResult(context));
8080
}
8181
}
82-
/// <summary>
83-
/// 生成错误提示信息。
84-
/// </summary>
85-
/// <param name="context"></param>
86-
/// <returns></returns>
87-
private ValidationResult CreateValidationResult(ValidationContext context)
82+
83+
private ValidationResult GetValidationResult(ValidationContext context)
8884
{
8985
var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options);
90-
var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName };
91-
return new ValidationResult(errorMessage, memberNames);
86+
return context.GetValidationResult(errorMessage);
9287
}
9388

94-
9589
/// <summary>
9690
/// 获得当前验证规则资源文件中 Key 格式
9791
/// </summary>

0 commit comments

Comments
 (0)