-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ValidateForm): add IValidatableObject support (#4260)
* 增加IValidatableObject支持 * 增加单元测试 * 补充demo模型 * 调整单元测试 * refactor: 更改模型名称 * chore: 更新字典 * chore: 移除不使用的命名空间 * doc: 更新示例 * fix: 修复字段更改时验证失效问题 * test: 更新单元测试 * doc: 格式化文档 * doc: 更新注释 * feat: 增加 IValidataResult 接口 * doc: 增加 IValidataResult 接口示例 * feat: 更新示例 * feat: 增加 ResetMemberNames 属性用于验证联动 * refactor: 调整时长防止脚本报错 * feat: 更新组件验证逻辑 * test: 更新单元测试 * refactor: 增加联动逻辑 * refactor: 精简代码 * feat: 增加验证失败联动逻辑 * refactor: 更改为 IValidateCollection 接口 * chore: 更新组件验证联动逻辑 * doc: 更新示例 * doc: 更新示例 * doc: 撤销更新准备还原类验证逻辑 * feat: 增加 IValidatableObject 逻辑支持 * refactor: 增加异常保护 * doc: 更新示例 * test: 更新单元测试 --------- Co-authored-by: Argo-AscioTech <[email protected]>
- Loading branch information
Showing
15 changed files
with
393 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,5 @@ autoplay | |
youtube | ||
vimeo | ||
scrlang | ||
Validata | ||
Validatable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
namespace BootstrapBlazor.Server.Data; | ||
|
||
/// <summary> | ||
/// 公司模型类 | ||
/// </summary> | ||
public class CustomValidataModel : IValidatableObject | ||
{ | ||
/// <summary> | ||
/// 名称 | ||
/// </summary> | ||
public string? Name { get; set; } | ||
|
||
/// <summary> | ||
/// 联系电话1 | ||
/// </summary> | ||
public string? Telephone1 { get; set; } | ||
|
||
/// <summary> | ||
/// 联系电话2 | ||
/// </summary> | ||
public string? Telephone2 { get; set; } | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <param name="validationContext"></param> | ||
/// <returns></returns> | ||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
{ | ||
if (string.Equals(Telephone1, Telephone2, StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
var localizer = validationContext.GetRequiredService<IStringLocalizer<CustomValidataModel>>(); | ||
yield return new ValidationResult(localizer["CanNotBeTheSame"], [nameof(Telephone1), nameof(Telephone2)]); | ||
} | ||
|
||
if (string.IsNullOrEmpty(Name)) | ||
{ | ||
yield return new ValidationResult("Name is required", [nameof(Name)]); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/BootstrapBlazor.Server/Data/CustomValidateCollectionModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
namespace BootstrapBlazor.Server.Data; | ||
|
||
/// <summary> | ||
/// 公司模型类 | ||
/// </summary> | ||
public class CustomValidateCollectionModel : IValidateCollection | ||
{ | ||
/// <summary> | ||
/// 联系电话1 | ||
/// </summary> | ||
[Display(Name = "联系电话1")] | ||
public string? Telephone1 { get; set; } | ||
|
||
/// <summary> | ||
/// 联系电话2 | ||
/// </summary> | ||
[Display(Name = "联系电话2")] | ||
public string? Telephone2 { get; set; } | ||
|
||
private readonly List<string> _validMemberNames = []; | ||
|
||
private readonly List<ValidationResult> _invalidMemberNames = []; | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <param name="validationContext"></param> | ||
/// <returns></returns> | ||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
{ | ||
_validMemberNames.Clear(); | ||
_invalidMemberNames.Clear(); | ||
if (string.Equals(Telephone1, Telephone2, StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
var localizer = validationContext.GetRequiredService<IStringLocalizer<CustomValidateCollectionModel>>(); | ||
var errorMessage = localizer["CanNotBeTheSame"]; | ||
if (validationContext.MemberName == nameof(Telephone1)) | ||
{ | ||
_invalidMemberNames.Add(new ValidationResult(errorMessage, [nameof(Telephone2)])); | ||
} | ||
else if (validationContext.MemberName == nameof(Telephone2)) | ||
{ | ||
_invalidMemberNames.Add(new ValidationResult(errorMessage, [nameof(Telephone1)])); | ||
} | ||
yield return new ValidationResult(errorMessage, [validationContext.MemberName!]); | ||
} | ||
else if (validationContext.MemberName == nameof(Telephone1)) | ||
{ | ||
_validMemberNames.Add(nameof(Telephone2)); | ||
|
||
} | ||
else if (validationContext.MemberName == nameof(Telephone2)) | ||
{ | ||
_validMemberNames.Add(nameof(Telephone1)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <returns></returns> | ||
public List<string> ValidMemberNames() => _validMemberNames; | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <returns></returns> | ||
public List<ValidationResult> InvalidMemberNames() => _invalidMemberNames; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/BootstrapBlazor/Components/Validate/IValidateCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
namespace BootstrapBlazor.Components; | ||
|
||
/// <summary> | ||
/// IValidateCollection 多个验证结果接口 支持组件间联动验证 | ||
/// </summary> | ||
public interface IValidateCollection | ||
{ | ||
/// <summary> | ||
/// 验证方法 | ||
/// </summary> | ||
/// <param name="validationContext"></param> | ||
/// <returns></returns> | ||
IEnumerable<ValidationResult> Validate(ValidationContext validationContext); | ||
|
||
/// <summary> | ||
/// 返回合法成员集合 | ||
/// </summary> | ||
/// <returns></returns> | ||
List<string> ValidMemberNames(); | ||
|
||
/// <summary> | ||
/// 返回非法成员集合 | ||
/// </summary> | ||
/// <returns></returns> | ||
List<ValidationResult> InvalidMemberNames(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.