Skip to content

Commit 1c2f3e0

Browse files
committed
refactor: 重构代码提高代码可读性
1 parent 34f1e9b commit 1c2f3e0

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,6 @@ private void ValidateDataAnnotations(object? value, ValidationContext context, L
385385
var result = rule.GetValidationResult(value, context);
386386
if (result != null && result != ValidationResult.Success)
387387
{
388-
// 查找 resource 资源文件中的 ErrorMessage
389-
var ruleNameSpan = rule.GetType().Name.AsSpan();
390-
var index = ruleNameSpan.IndexOf(attributeSpan, StringComparison.OrdinalIgnoreCase);
391-
var ruleName = ruleNameSpan[..index];
392388
var find = false;
393389
if (!string.IsNullOrEmpty(rule.ErrorMessage))
394390
{
@@ -400,29 +396,36 @@ private void ValidateDataAnnotations(object? value, ValidationContext context, L
400396
}
401397
}
402398

403-
// 通过设置 ErrorMessage 检索
404-
if (!context.ObjectType.Assembly.IsDynamic && !find
405-
&& !string.IsNullOrEmpty(rule.ErrorMessage)
406-
&& LocalizerFactory.Create(context.ObjectType).TryGetLocalizerString(rule.ErrorMessage, out var msg))
399+
if (!context.ObjectType.Assembly.IsDynamic)
407400
{
408-
rule.ErrorMessage = msg;
409-
find = true;
410-
}
401+
if (!find && !string.IsNullOrEmpty(rule.ErrorMessage)
402+
&& LocalizerFactory.Create(context.ObjectType).TryGetLocalizerString(rule.ErrorMessage, out var msg))
403+
{
404+
// 通过设置 ErrorMessage 检索
405+
rule.ErrorMessage = msg;
406+
find = true;
407+
}
411408

412-
// 通过 Attribute 检索
413-
if (!rule.GetType().Assembly.IsDynamic && !find
414-
&& LocalizerFactory.Create(rule.GetType()).TryGetLocalizerString(nameof(rule.ErrorMessage), out msg))
415-
{
416-
rule.ErrorMessage = msg;
417-
find = true;
418-
}
409+
if (!find && LocalizerFactory.Create(rule.GetType()).TryGetLocalizerString(nameof(rule.ErrorMessage), out msg))
410+
{
411+
// 通过 Attribute 检索
412+
rule.ErrorMessage = msg;
413+
find = true;
414+
}
419415

420-
// 通过 字段.规则名称 检索
421-
if (!context.ObjectType.Assembly.IsDynamic && !find
422-
&& LocalizerFactory.Create(context.ObjectType).TryGetLocalizerString($"{memberName}.{ruleName.ToString()}", out msg))
423-
{
424-
rule.ErrorMessage = msg;
425-
find = true;
416+
if (!find)
417+
{
418+
// 通过 字段.规则名称 检索
419+
// 查找 resource 资源文件中的 ErrorMessage
420+
var ruleNameSpan = rule.GetType().Name.AsSpan();
421+
var index = ruleNameSpan.IndexOf(attributeSpan, StringComparison.OrdinalIgnoreCase);
422+
var ruleName = ruleNameSpan[..index];
423+
if (LocalizerFactory.Create(context.ObjectType).TryGetLocalizerString($"{memberName}.{ruleName.ToString()}", out msg))
424+
{
425+
rule.ErrorMessage = msg;
426+
find = true;
427+
}
428+
}
426429
}
427430

428431
if (!find)

0 commit comments

Comments
 (0)