Skip to content

Commit 03ebf66

Browse files
authored
fix(ValidateBase): do not add validate state when ErrorMessage is null (#7463)
* refactor: 重构代码 * refactor: 重构代码 * chore: bump version 10.2.1-beta04 * test: 增加单元测试 * refactor: 重构代码 * test: 更新单元测试
1 parent c50ef3f commit 03ebf66

5 files changed

Lines changed: 125 additions & 3 deletions

File tree

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>10.2.1-beta03</Version>
4+
<Version>10.2.1-beta04</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue
281281

282282
// support SelectedItem? type
283283
result = SelectedItem != null ? (TValue)(object)SelectedItem : default;
284-
validationErrorMessage = "";
284+
validationErrorMessage = null;
285285
return SelectedItem != null;
286286
}
287287

src/BootstrapBlazor/Components/Validate/ValidateBase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ protected string CurrentValueAsString
119119
PreviousParsingAttemptFailed = false;
120120
CurrentValue = parsedValue;
121121
}
122+
else if (string.IsNullOrEmpty(validationErrorMessage))
123+
{
124+
// validationErrorMessage 为 null 表示转换目标值失败组件值未改变
125+
PreviousParsingAttemptFailed = false;
126+
}
122127
else
123128
{
124129
PreviousParsingAttemptFailed = true;
@@ -131,7 +136,7 @@ protected string CurrentValueAsString
131136

132137
if (FieldIdentifier != null)
133138
{
134-
_parsingValidationMessages?.Add(FieldIdentifier.Value, PreviousErrorMessage ?? "");
139+
_parsingValidationMessages?.Add(FieldIdentifier.Value, PreviousErrorMessage);
135140

136141
// Since we're not writing to CurrentValue, we'll need to notify about modification from here
137142
EditContext?.NotifyFieldChanged(FieldIdentifier.Value);

test/UnitTest/Components/DateTimePickerTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,41 @@ await cut.InvokeAsync(() =>
12551255
Assert.Equal("02/15/2024 01:00:00", cut.Instance.Value.ToString("MM/dd/yyyy HH:mm:ss"));
12561256
}
12571257

1258+
[Fact]
1259+
public async Task ValidateForm_IsEditable_Ok()
1260+
{
1261+
var model = new Foo() { DateTime = new DateTime(2024, 2, 15) };
1262+
var cut = Context.Render<ValidateForm>(pb =>
1263+
{
1264+
pb.Add(a => a.Model, model);
1265+
pb.AddChildContent<DateTimePicker<DateTime?>>(builder =>
1266+
{
1267+
builder.Add(a => a.IsEditable, true);
1268+
builder.Add(a => a.ViewMode, DatePickerViewMode.Date);
1269+
builder.Add(a => a.DateFormat, "MM/dd/yyyy");
1270+
builder.Add(a => a.Value, model.DateTime);
1271+
builder.Add(a => a.ValueChanged, EventCallback.Factory.Create<DateTime?>(this, v =>
1272+
{
1273+
model.DateTime = v;
1274+
}));
1275+
builder.Add(a => a.ValueExpression, Utility.GenerateValueExpression(model, nameof(model.DateTime), typeof(DateTime?)));
1276+
});
1277+
});
1278+
var input = cut.Find(".datetime-picker-input");
1279+
1280+
// 更改成非法数值 测试 CurrentValueAsString 赋值逻辑
1281+
await cut.InvokeAsync(() =>
1282+
{
1283+
input.Change("00/15/2024");
1284+
});
1285+
Assert.NotNull(model.DateTime);
1286+
Assert.Equal("02/15/2024", model.DateTime.Value.ToString("MM/dd/yyyy"));
1287+
1288+
// 录入非法数值 Validate 通过
1289+
var valid = cut.Instance.Validate();
1290+
Assert.True(valid);
1291+
}
1292+
12581293
[Fact]
12591294
public void MinValueToEmpty_Ok()
12601295
{

test/UnitTest/Components/InputNumberTest.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,88 @@ public void Type_Ok(Type t)
216216
cut.InvokeAsync(() => buttons[1].Click());
217217
}
218218

219+
[Fact]
220+
public async Task Validate_Ok()
221+
{
222+
var model = new Foo() { Count = 1 };
223+
var cut = Context.Render<ValidateForm>(pb =>
224+
{
225+
pb.Add(a => a.Model, model);
226+
pb.AddChildContent<BootstrapInputNumber<int>>(builder =>
227+
{
228+
builder.Add(a => a.Value, model.Count);
229+
builder.Add(a => a.ValueChanged, EventCallback.Factory.Create<int>(this, v =>
230+
{
231+
model.Count = v;
232+
}));
233+
builder.Add(a => a.ValueExpression, Utility.GenerateValueExpression(model, nameof(model.Count), typeof(int)));
234+
});
235+
});
236+
var input = cut.Find(".form-control");
237+
238+
// 更改成非法数值 测试 CurrentValueAsString 赋值逻辑
239+
await cut.InvokeAsync(() =>
240+
{
241+
input.Change("t");
242+
});
243+
Assert.Equal(1, model.Count);
244+
245+
var valid = cut.Instance.Validate();
246+
Assert.False(valid);
247+
248+
await cut.InvokeAsync(() =>
249+
{
250+
input.Change("t2");
251+
});
252+
Assert.Equal(1, model.Count);
253+
valid = cut.Instance.Validate();
254+
Assert.False(valid);
255+
256+
await cut.InvokeAsync(() =>
257+
{
258+
input.Change("2");
259+
});
260+
Assert.Equal(2, model.Count);
261+
262+
valid = cut.Instance.Validate();
263+
Assert.True(valid);
264+
}
265+
266+
[Fact]
267+
public async Task TryParseValueFromString_Ok()
268+
{
269+
var model = new Foo() { Count = 1 };
270+
var cut = Context.Render<BootstrapInputNumber<int>>(pb =>
271+
{
272+
pb.Add(a => a.Value, 1);
273+
pb.Add(a => a.ValueChanged, EventCallback.Factory.Create<int>(this, v =>
274+
{
275+
model.Count = v;
276+
}));
277+
pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(model, nameof(model.Count), typeof(int)));
278+
});
279+
var input = cut.Find(".form-control");
280+
281+
// 更改成非法数值 测试 CurrentValueAsString 赋值逻辑
282+
await cut.InvokeAsync(() =>
283+
{
284+
input.Change("t");
285+
});
286+
Assert.Equal(1, model.Count);
287+
288+
await cut.InvokeAsync(() =>
289+
{
290+
input.Change("t2");
291+
});
292+
Assert.Equal(1, model.Count);
293+
294+
await cut.InvokeAsync(() =>
295+
{
296+
input.Change("2");
297+
});
298+
Assert.Equal(2, model.Count);
299+
}
300+
219301
private class Cat
220302
{
221303
[Range(1, 10)]

0 commit comments

Comments
 (0)