Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/BootstrapBlazor/Validators/RequiredValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Localization;
using System.Collections;
using System.Globalization;
using System.Security.AccessControl;

namespace BootstrapBlazor.Components;

Expand Down Expand Up @@ -53,17 +54,15 @@ public override void Validate(object? propertyValue, ValidationContext context,
ErrorMessage = l.Value;
}
}
var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options);
var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName };
if (propertyValue == null)
{
results.Add(new ValidationResult(errorMessage, memberNames));
results.Add(CreateValidationResult(context));
}
else if (propertyValue is string val)
{
if (!AllowEmptyString && val == string.Empty)
{
results.Add(new ValidationResult(errorMessage, memberNames));
results.Add(CreateValidationResult(context));
}
}
else if (propertyValue is IEnumerable v)
Expand All @@ -72,14 +71,26 @@ public override void Validate(object? propertyValue, ValidationContext context,
var valid = enumerator.MoveNext();
if (!valid)
{
results.Add(new ValidationResult(errorMessage, memberNames));
results.Add(CreateValidationResult(context));
}
}
else if (propertyValue is DateTimeRangeValue dv && dv is { NullStart: null, NullEnd: null })
{
results.Add(new ValidationResult(errorMessage, memberNames));
results.Add(CreateValidationResult(context));
}
}
/// <summary>
/// 生成错误提示信息。
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private ValidationResult CreateValidationResult(ValidationContext context)
{
var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options);
var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName };
return new ValidationResult(errorMessage, memberNames);
}


/// <summary>
/// 获得当前验证规则资源文件中 Key 格式
Expand Down
Loading