Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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>10.2.1-beta03</Version>
<Version>10.2.1-beta04</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Select/Select.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue

// support SelectedItem? type
result = SelectedItem != null ? (TValue)(object)SelectedItem : default;
validationErrorMessage = "";
validationErrorMessage = null;
return SelectedItem != null;
}

Expand Down
7 changes: 6 additions & 1 deletion src/BootstrapBlazor/Components/Validate/ValidateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ protected string CurrentValueAsString
PreviousParsingAttemptFailed = false;
CurrentValue = parsedValue;
}
else if (string.IsNullOrEmpty(validationErrorMessage))
{
// validationErrorMessage 为 null 表示转换目标值失败组件值未改变
PreviousParsingAttemptFailed = false;
}
else
{
PreviousParsingAttemptFailed = true;
Expand All @@ -131,7 +136,7 @@ protected string CurrentValueAsString

if (FieldIdentifier != null)
{
_parsingValidationMessages?.Add(FieldIdentifier.Value, PreviousErrorMessage ?? "");
_parsingValidationMessages?.Add(FieldIdentifier.Value, PreviousErrorMessage);

// Since we're not writing to CurrentValue, we'll need to notify about modification from here
EditContext?.NotifyFieldChanged(FieldIdentifier.Value);
Expand Down
35 changes: 35 additions & 0 deletions test/UnitTest/Components/DateTimePickerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,41 @@ await cut.InvokeAsync(() =>
Assert.Equal("02/15/2024 01:00:00", cut.Instance.Value.ToString("MM/dd/yyyy HH:mm:ss"));
}

[Fact]
public async Task ValidateForm_IsEditable_Ok()
{
var model = new Foo() { DateTime = new DateTime(2024, 2, 15) };
var cut = Context.Render<ValidateForm>(pb =>
{
pb.Add(a => a.Model, model);
pb.AddChildContent<DateTimePicker<DateTime?>>(builder =>
{
builder.Add(a => a.IsEditable, true);
Comment on lines +1258 to +1267
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Extend the DateTimePicker validation test to assert the absence of validation messages for the field after invalid editable input.

In ValidateForm_IsEditable_Ok, you already confirm the model value is unchanged and Validate() returns true after an invalid editable value. To better cover the change that skips adding validation state when the error message is null, please also assert that EditContext.GetValidationMessages for the DateTime field is empty. This verifies the field itself is not marked invalid despite the parse failure and strengthens future validation regression coverage.

Suggested implementation:

    [Fact]
    public async Task ValidateForm_IsEditable_Ok()
    {
        var model = new Foo() { DateTime = new DateTime(2024, 2, 15) };
        var cut = Context.Render<ValidateForm>(pb =>
        {
            pb.Add(a => a.Model, model);
            pb.AddChildContent<DateTimePicker<DateTime?>>(builder =>
            {
                builder.Add(a => a.IsEditable, true);
                builder.Add(a => a.ViewMode, DatePickerViewMode.Date);

To implement the suggestion, inside ValidateForm_IsEditable_Ok after you simulate an invalid editable input, assert the model value is unchanged, and call Validate() (or cut.Instance.EditContext.Validate()) and assert it returns true, add an assertion that the EditContext has no validation messages for the DateTime field.

Concretely, right after your existing assertion that validation succeeds, add something along these lines:

// existing code (example)
var editContext = cut.Instance.EditContext;
var isValid = editContext.Validate();
Assert.True(isValid);
Assert.Equal(new DateTime(2024, 2, 15), model.DateTime); // existing assertion

// new assertion: no validation messages for the DateTime field
var messages = editContext.GetValidationMessages(
    FieldIdentifier.Create(() => model.DateTime)
);
Assert.Empty(messages);

If you are using the lambda-based overload of GetValidationMessages, you can also write:

Assert.Empty(editContext.GetValidationMessages(() => model.DateTime));

Make sure editContext is the same EditContext instance you already use for Validate(), and keep this new assertion after the invalid editable input has been processed to ensure the field itself is not marked invalid despite the parse failure.

builder.Add(a => a.ViewMode, DatePickerViewMode.Date);
builder.Add(a => a.DateFormat, "MM/dd/yyyy");
builder.Add(a => a.Value, model.DateTime);
builder.Add(a => a.ValueChanged, EventCallback.Factory.Create<DateTime?>(this, v =>
{
model.DateTime = v;
}));
builder.Add(a => a.ValueExpression, Utility.GenerateValueExpression(model, nameof(model.DateTime), typeof(DateTime?)));
});
});
var input = cut.Find(".datetime-picker-input");

// 更改成非法数值 测试 CurrentValueAsString 赋值逻辑
await cut.InvokeAsync(() =>
{
input.Change("00/15/2024");
});
Assert.NotNull(model.DateTime);
Assert.Equal("02/15/2024", model.DateTime.Value.ToString("MM/dd/yyyy"));

// 录入非法数值 Validate 通过
var valid = cut.Instance.Validate();
Assert.True(valid);
}

[Fact]
public void MinValueToEmpty_Ok()
{
Expand Down
82 changes: 82 additions & 0 deletions test/UnitTest/Components/InputNumberTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,88 @@ public void Type_Ok(Type t)
cut.InvokeAsync(() => buttons[1].Click());
}

[Fact]
public async Task Validate_Ok()
{
var model = new Foo() { Count = 1 };
var cut = Context.Render<ValidateForm>(pb =>
{
pb.Add(a => a.Model, model);
pb.AddChildContent<BootstrapInputNumber<int>>(builder =>
{
builder.Add(a => a.Value, model.Count);
builder.Add(a => a.ValueChanged, EventCallback.Factory.Create<int>(this, v =>
{
model.Count = v;
}));
builder.Add(a => a.ValueExpression, Utility.GenerateValueExpression(model, nameof(model.Count), typeof(int)));
});
});
var input = cut.Find(".form-control");

// 更改成非法数值 测试 CurrentValueAsString 赋值逻辑
await cut.InvokeAsync(() =>
{
input.Change("t");
});
Assert.Equal(1, model.Count);

var valid = cut.Instance.Validate();
Assert.False(valid);

await cut.InvokeAsync(() =>
{
input.Change("t2");
});
Assert.Equal(1, model.Count);
valid = cut.Instance.Validate();
Assert.False(valid);

await cut.InvokeAsync(() =>
{
input.Change("2");
});
Assert.Equal(2, model.Count);

valid = cut.Instance.Validate();
Assert.True(valid);
}

[Fact]
public async Task TryParseValueFromString_Ok()
{
var model = new Foo() { Count = 1 };
var cut = Context.Render<BootstrapInputNumber<int>>(pb =>
{
pb.Add(a => a.Value, 1);
pb.Add(a => a.ValueChanged, EventCallback.Factory.Create<int>(this, v =>
{
model.Count = v;
}));
pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(model, nameof(model.Count), typeof(int)));
});
var input = cut.Find(".form-control");

// 更改成非法数值 测试 CurrentValueAsString 赋值逻辑
await cut.InvokeAsync(() =>
{
input.Change("t");
});
Assert.Equal(1, model.Count);

await cut.InvokeAsync(() =>
{
input.Change("t2");
});
Assert.Equal(1, model.Count);

await cut.InvokeAsync(() =>
{
input.Change("2");
});
Assert.Equal(2, model.Count);
}

private class Cat
{
[Range(1, 10)]
Expand Down
Loading