Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.2.0</Version>
<Version>9.2.1-beta03</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions src/BootstrapBlazor/Extensions/ValidateContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ public static class ValidationContextExtensions
}
return ret;
}

/// <summary>
/// 获得 <see cref="ValidationResult"/> 实例
/// </summary>
/// <param name="context"></param>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static ValidationResult GetValidationResult(this ValidationContext context, string? errorMessage)
{
var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName };
return new ValidationResult(errorMessage, memberNames);
}
}
16 changes: 10 additions & 6 deletions src/BootstrapBlazor/Validators/RequiredValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,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(GetValidationResult(context));
}
else if (propertyValue is string val)
{
if (!AllowEmptyString && val == string.Empty)
{
results.Add(new ValidationResult(errorMessage, memberNames));
results.Add(GetValidationResult(context));
}
}
else if (propertyValue is IEnumerable v)
Expand All @@ -72,15 +70,21 @@ public override void Validate(object? propertyValue, ValidationContext context,
var valid = enumerator.MoveNext();
if (!valid)
{
results.Add(new ValidationResult(errorMessage, memberNames));
results.Add(GetValidationResult(context));
}
}
else if (propertyValue is DateTimeRangeValue dv && dv is { NullStart: null, NullEnd: null })
{
results.Add(new ValidationResult(errorMessage, memberNames));
results.Add(GetValidationResult(context));
}
}

private ValidationResult GetValidationResult(ValidationContext context)
{
var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options);
return context.GetValidationResult(errorMessage);
}

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