Skip to content

Commit d123c74

Browse files
ArgoZhangOcrosoft
andauthored
feat(FieldIdentifier): add GetRequiredValidator extension method (#4937)
* refactor: 代码重构 * chore: bump version 9.1.9-beta05 Co-Authored-By: Ocrosoft <[email protected]> * test: 增加单元测试 * refactor: 更新必填项验证逻辑 Co-Authored-By: Ocrosoft <[email protected]> * refactor: 利用基类方法精简代码 * refactor: 增加 GetRequiredValidator 扩展方法 * refactor: 支持 DateTimeRangeValue 合规性检查 * test: 增加必填项约束 * test: 更新单元测试 * test: 更新单元测试 * doc: 更新示例 --------- Co-authored-by: Ocrosoft <[email protected]>
1 parent d651d08 commit d123c74

File tree

18 files changed

+185
-150
lines changed

18 files changed

+185
-150
lines changed

src/BootstrapBlazor.Server/Components/Samples/SelectTrees.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<ValidateForm Model="BindModel">
4040
<div class="row g-3">
4141
<div class="col-12 col-sm-6">
42-
<SelectTree TValue="string" @bind-Value="BindModel.Text" ShowIcon="true" Items="BindItems" />
42+
<SelectTree TValue="string" @bind-Value="BindModel.Text" ShowIcon="true" Items="BindItems" OnExpandNodeAsync="OnExpandNodeAsync" />
4343
</div>
4444
<div class="col-12 col-sm-6 align-self-end">
4545
<Button ButtonType="ButtonType.Submit">@Localizer["SelectTreesClientValidationButtonText"]</Button>

src/BootstrapBlazor.Server/Components/Samples/SelectTrees.razor.cs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class SelectTrees
1919
[NotNull]
2020
private TreeFoo? Model { get; set; }
2121

22-
private TreeFoo BindModel { get; set; } = new TreeFoo();
22+
private TreeFoo BindModel { get; set; } = new();
2323

2424
[NotNull]
2525
private List<TreeViewItem<string>>? BindItems { get; set; }
@@ -55,35 +55,49 @@ protected override void OnInitialized()
5555

5656
BindModel = new TreeFoo()
5757
{
58-
Text = "Sub Menu Three",
5958
Id = "1090",
6059
ParentId = "1050",
61-
Icon = "fa-solid fa-font-awesome",
62-
IsActive = true
60+
Icon = "fa-solid fa-font-awesome"
6361
};
6462

6563
BindItems =
6664
[
67-
new TreeViewItem<string>("目录一")
68-
{
69-
Text ="目录一",
70-
Icon = "fa-solid fa-folder",
71-
ExpandIcon = "fa-solid fa-folder-open",
72-
Items =
73-
[
74-
new TreeViewItem<string>("子目录一")
75-
{
76-
Text ="子目录一",
77-
Icon = "fa-solid fa-folder",
78-
ExpandIcon = "fa-solid fa-folder-open",
79-
Items =
80-
[
81-
new TreeViewItem<string>("文件一") { Text = "文件一", Icon = "fa-solid fa-file", IsActive = true },
82-
new TreeViewItem<string>("文件二") { Text = "文件二", Icon = "fa-solid fa-file" }
83-
]
84-
}
85-
]
86-
}
87-
];
65+
new TreeViewItem<string>("目录一")
66+
{
67+
Text = "目录一",
68+
Icon = "fa-solid fa-folder",
69+
ExpandIcon = "fa-solid fa-folder-open",
70+
Value = "101",
71+
HasChildren = true
72+
}
73+
];
74+
}
75+
76+
private async Task<IEnumerable<TreeViewItem<string>>> OnExpandNodeAsync(TreeViewItem<string> node)
77+
{
78+
await Task.Delay(500);
79+
80+
if (node.Value == "101")
81+
{
82+
return [new TreeViewItem<string>("子目录一")
83+
{
84+
Text = "子目录一",
85+
Icon = "fa-solid fa-folder",
86+
ExpandIcon = "fa-solid fa-folder-open",
87+
Value = "1001",
88+
HasChildren = true
89+
}];
90+
}
91+
else if (node.Value == "1001")
92+
{
93+
return [
94+
new TreeViewItem<string>("文件一") { Text = "文件一", Icon = "fa-solid fa-file", Value = "10010" },
95+
new TreeViewItem<string>("文件二") { Text = "文件二", Icon = "fa-solid fa-file", Value = "10011" }
96+
];
97+
}
98+
else
99+
{
100+
return [];
101+
}
88102
}
89103
}

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.1.9-beta04</Version>
4+
<Version>9.1.9-beta05</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Checkbox/CheckboxList.razor.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ public partial class CheckboxList<TValue> : ValidateBase<TValue>
9898
[Parameter]
9999
public Func<Task>? OnMaxSelectedCountExceed { get; set; }
100100

101-
[Inject]
102-
[NotNull]
103-
private IStringLocalizerFactory? LocalizerFactory { get; set; }
104-
105101
/// <summary>
106102
/// 获得 当前选项是否被禁用
107103
/// </summary>
@@ -121,23 +117,7 @@ protected override void OnInitialized()
121117
EnsureParameterValid();
122118

123119
// 处理 Required 标签
124-
if (EditContext != null && FieldIdentifier != null)
125-
{
126-
var pi = FieldIdentifier.Value.Model.GetType().GetPropertyByName(FieldIdentifier.Value.FieldName);
127-
if (pi != null)
128-
{
129-
var required = pi.GetCustomAttribute<RequiredAttribute>(true);
130-
if (required != null)
131-
{
132-
Rules.Add(new RequiredValidator()
133-
{
134-
LocalizerFactory = LocalizerFactory,
135-
ErrorMessage = required.ErrorMessage,
136-
AllowEmptyString = required.AllowEmptyStrings
137-
});
138-
}
139-
}
140-
}
120+
AddRequiredValidator();
141121
}
142122

143123
/// <summary>

src/BootstrapBlazor/Components/Checkbox/CheckboxListGeneric.razor.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Microsoft.Extensions.Localization;
7-
using System.Reflection;
87

98
namespace BootstrapBlazor.Components;
109

@@ -111,10 +110,6 @@ public partial class CheckboxListGeneric<TValue> : IModelEqualityComparer<TValue
111110
[Parameter]
112111
public Func<Task>? OnMaxSelectedCountExceed { get; set; }
113112

114-
[Inject]
115-
[NotNull]
116-
private IStringLocalizerFactory? LocalizerFactory { get; set; }
117-
118113
/// <summary>
119114
/// 获得 当前选项是否被禁用
120115
/// </summary>
@@ -132,23 +127,7 @@ protected override void OnInitialized()
132127
base.OnInitialized();
133128

134129
// 处理 Required 标签
135-
if (EditContext != null && FieldIdentifier != null)
136-
{
137-
var pi = FieldIdentifier.Value.Model.GetType().GetPropertyByName(FieldIdentifier.Value.FieldName);
138-
if (pi != null)
139-
{
140-
var required = pi.GetCustomAttribute<RequiredAttribute>(true);
141-
if (required != null)
142-
{
143-
Rules.Add(new RequiredValidator()
144-
{
145-
LocalizerFactory = LocalizerFactory,
146-
ErrorMessage = required.ErrorMessage,
147-
AllowEmptyString = required.AllowEmptyStrings
148-
});
149-
}
150-
}
151-
}
130+
AddRequiredValidator();
152131
}
153132

154133
/// <summary>

src/BootstrapBlazor/Components/DateTimeRange/DateTimeRange.razor.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,6 @@ public bool AllowNull
256256
[NotNull]
257257
private IStringLocalizer<DateTimeRange>? Localizer { get; set; }
258258

259-
[Inject]
260-
[NotNull]
261-
private IStringLocalizerFactory? LocalizerFactory { get; set; }
262-
263259
[Inject]
264260
[NotNull]
265261
private IIconTheme? IconTheme { get; set; }
@@ -280,24 +276,7 @@ protected override void OnInitialized()
280276
{
281277
base.OnInitialized();
282278

283-
if (FieldIdentifier != null)
284-
{
285-
var pi = FieldIdentifier.Value.Model.GetType().GetPropertyByName(FieldIdentifier.Value.FieldName);
286-
if (pi != null)
287-
{
288-
var required = pi.GetCustomAttribute<RequiredAttribute>(true);
289-
if (required != null)
290-
{
291-
Rules.Add(new DateTimeRangeRequiredValidator()
292-
{
293-
LocalizerFactory = LocalizerFactory,
294-
ErrorMessage = required.ErrorMessage,
295-
AllowEmptyString = required.AllowEmptyStrings
296-
});
297-
}
298-
}
299-
}
300-
279+
AddRequiredValidator();
301280
_showLeftButtons = RenderMode == DateTimeRangeRenderMode.Single;
302281
}
303282

src/BootstrapBlazor/Components/Select/SelectObject.razor.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,6 @@ public partial class SelectObject<TItem>
111111
[NotNull]
112112
private IStringLocalizer<Select<TItem>>? Localizer { get; set; }
113113

114-
/// <summary>
115-
/// 获得/设置 IStringLocalizerFactory 注入服务实例 默认为 null
116-
/// </summary>
117-
[Inject]
118-
[NotNull]
119-
public IStringLocalizerFactory? LocalizerFactory { get; set; }
120-
121114
/// <summary>
122115
/// 获得 input 组件 Id 方法
123116
/// </summary>
@@ -140,10 +133,7 @@ protected override void OnInitialized()
140133
{
141134
base.OnInitialized();
142135

143-
if (ValidateForm != null)
144-
{
145-
Rules.Add(new RequiredValidator() { LocalizerFactory = LocalizerFactory, ErrorMessage = "{0} is required." });
146-
}
136+
AddRequiredValidator();
147137
_context = new InternalSelectObjectContext<TItem>() { Component = this };
148138
}
149139

src/BootstrapBlazor/Components/Select/SelectTable.razor.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,6 @@ namespace BootstrapBlazor.Components;
178178
[NotNull]
179179
private IStringLocalizer<Select<TItem>>? Localizer { get; set; }
180180

181-
/// <summary>
182-
/// 获得/设置 IStringLocalizerFactory 注入服务实例 默认为 null
183-
/// </summary>
184-
[Inject]
185-
[NotNull]
186-
public IStringLocalizerFactory? LocalizerFactory { get; set; }
187-
188181
/// <summary>
189182
/// 获得 input 组件 Id 方法
190183
/// </summary>
@@ -205,10 +198,7 @@ protected override void OnInitialized()
205198
{
206199
base.OnInitialized();
207200

208-
if (ValidateForm != null)
209-
{
210-
Rules.Add(new RequiredValidator() { LocalizerFactory = LocalizerFactory, ErrorMessage = "{0} is required." });
211-
}
201+
AddRequiredValidator();
212202
}
213203

214204
/// <summary>

src/BootstrapBlazor/Components/Select/SelectTree.razor.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ public partial class SelectTree<TValue> : IModelEqualityComparer<TValue>
155155
[NotNull]
156156
private IIconTheme? IconTheme { get; set; }
157157

158+
/// <summary>
159+
/// <inheritdoc/>
160+
/// </summary>
161+
protected override void OnInitialized()
162+
{
163+
base.OnInitialized();
164+
165+
// 处理 Required 标签
166+
AddRequiredValidator();
167+
}
168+
158169
/// <summary>
159170
/// <inheritdoc/>
160171
/// </summary>
@@ -171,12 +182,20 @@ protected override async Task OnInitializedAsync()
171182
/// <summary>
172183
/// <inheritdoc/>
173184
/// </summary>
174-
protected override async Task OnParametersSetAsync()
185+
protected override void OnParametersSet()
175186
{
176-
await base.OnParametersSetAsync();
187+
base.OnParametersSet();
177188

178189
DropdownIcon ??= IconTheme.GetIconByKey(ComponentIcons.SelectTreeDropdownIcon);
179190
PlaceHolder ??= Localizer[nameof(PlaceHolder)];
191+
}
192+
193+
/// <summary>
194+
/// <inheritdoc/>
195+
/// </summary>
196+
protected override async Task OnParametersSetAsync()
197+
{
198+
await base.OnParametersSetAsync();
180199

181200
Items ??= [];
182201

src/BootstrapBlazor/Components/Transfer/Transfer.razor.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,6 @@ public partial class Transfer<TValue>
188188
[Parameter]
189189
public RenderFragment<SelectedItem>? RightItemTemplate { get; set; }
190190

191-
/// <summary>
192-
/// 获得/设置 IStringLocalizerFactory 注入服务实例 默认为 null
193-
/// </summary>
194-
[Inject]
195-
[NotNull]
196-
public IStringLocalizerFactory? LocalizerFactory { get; set; }
197-
198191
[Inject]
199192
[NotNull]
200193
private IIconTheme? IconTheme { get; set; }
@@ -206,21 +199,8 @@ protected override void OnInitialized()
206199
{
207200
base.OnInitialized();
208201

209-
OnSetItemClass ??= _ => null;
210-
211202
// 处理 Required 标签
212-
if (FieldIdentifier != null)
213-
{
214-
var pi = FieldIdentifier.Value.Model.GetType().GetPropertyByName(FieldIdentifier.Value.FieldName);
215-
if (pi != null)
216-
{
217-
var required = pi.GetCustomAttribute<RequiredAttribute>(true);
218-
if (required != null)
219-
{
220-
Rules.Add(new RequiredValidator() { LocalizerFactory = LocalizerFactory, ErrorMessage = required.ErrorMessage, AllowEmptyString = required.AllowEmptyStrings });
221-
}
222-
}
223-
}
203+
AddRequiredValidator();
224204
}
225205

226206
/// <summary>

0 commit comments

Comments
 (0)