Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(IValidateComponent): remove ToggleMessage validProperty parameter #4252

Merged
merged 9 commits into from
Sep 10, 2024
260 changes: 130 additions & 130 deletions src/BootstrapBlazor.Server/Components/Samples/ValidateForms.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,143 +33,143 @@
<div class="col-12">
<BootstrapInputGroup>
<BootstrapInputGroupLabel ShowRequiredMark DisplayText="Test"></BootstrapInputGroupLabel>
<BootstrapInput @bind-Value="@Model1.Name" DisplayText="@Localizer["LongDisplayText"]" />
</BootstrapInputGroup>
</div>
</div>
<div class="row g-3 form-inline mt-0">
<div class="col-12">
<BootstrapInput @bind-Value="@Model1.Address" DisplayText="@Localizer["LongDisplayText"]" ShowLabelTooltip="true" />
</div>
<div class="col-12">
<Button ButtonType="ButtonType.Submit" Icon="fa-fw fa-solid fa-floppy-disk" Text="@Localizer["ValidateFormsSubmitButtonText"]" IsAsync="true"></Button>
</div>
</div>
</ValidateForm>
<ConsoleLogger @ref="Logger1" />
</DemoBlock>
<BootstrapInput @bind-Value="@Model1.Name" DisplayText="@Localizer["LongDisplayText"]" />
</BootstrapInputGroup>
</div>
</div>
<div class="row g-3 form-inline mt-0">
<div class="col-12">
<BootstrapInput @bind-Value="@Model1.Address" DisplayText="@Localizer["LongDisplayText"]" ShowLabelTooltip="true" />
</div>
<div class="col-12">
<Button ButtonType="ButtonType.Submit" Icon="fa-fw fa-solid fa-floppy-disk" Text="@Localizer["ValidateFormsSubmitButtonText"]" IsAsync="true"></Button>
</div>
</div>
</ValidateForm>
<ConsoleLogger @ref="Logger1" />
</DemoBlock>

<DemoBlock Title="@Localizer["ValidateFormInnerComponentTitle"]" Introduction="@Localizer["ValidateFormInnerComponentIntro"]" Name="InnerComponent">
<section ignore>
<p class="legend">@Localizer["ValidateFormInnerComponentInnerComponentLabel"]</p>
<p>@Localizer["ValidateFormInnerComponentInnerComponentDescription1"]</p>
<ul class="ul-demo mb-3">
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi1"]</li>
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi2"]</li>
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi3"]</li>
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi4"]</li>
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi5"]</li>
</ul>
<p>@((MarkupString)Localizer["ValidateFormInnerComponentInnerComponentDescription2"].Value)</p>
</section>
<DemoBlock Title="@Localizer["ValidateFormInnerComponentTitle"]" Introduction="@Localizer["ValidateFormInnerComponentIntro"]" Name="InnerComponent">
<section ignore>
<p class="legend">@Localizer["ValidateFormInnerComponentInnerComponentLabel"]</p>
<p>@Localizer["ValidateFormInnerComponentInnerComponentDescription1"]</p>
<ul class="ul-demo mb-3">
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi1"]</li>
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi2"]</li>
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi3"]</li>
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi4"]</li>
<li>@Localizer["ValidateFormInnerComponentInnerComponentLi5"]</li>
</ul>
<p>@((MarkupString)Localizer["ValidateFormInnerComponentInnerComponentDescription2"].Value)</p>
</section>

<ValidateForm Model="@Model2" OnValidSubmit="@OnValidSubmit2" OnInvalidSubmit="@OnInvalidSubmit2">
<div class="row g-3">
<div class="col-12 col-sm-6">
<BootstrapInputGroupLabel @bind-Value="@Model2.Name" />
<BootstrapInputGroup>
<Display @bind-Value="@Model2.Name"></Display>
<BootstrapInputGroupLabel DisplayText="Test" />
</BootstrapInputGroup>
</div>
<div class="col-12 col-sm-6">
<BootstrapInputNumber @bind-Value="@Model2.Count" />
</div>
<div class="col-12 col-sm-6">
<DateTimePicker @bind-Value="@Model2.DateTime" />
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="@Model2.Education" />
</div>
<div class="col-12">
<CheckboxList @bind-Value="@Model2.Hobby" Items="@Hobbies2" />
</div>
<div class="col-12">
<Button ButtonType="@ButtonType.Submit" Icon="fa-fw fa-solid fa-floppy-disk" IsAsync="true" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
</div>
</div>
</ValidateForm>
<ConsoleLogger @ref="Logger2" />
</DemoBlock>
<ValidateForm Model="@Model2" OnValidSubmit="@OnValidSubmit2" OnInvalidSubmit="@OnInvalidSubmit2">
<div class="row g-3">
<div class="col-12 col-sm-6">
<BootstrapInputGroupLabel @bind-Value="@Model2.Name" />
<BootstrapInputGroup>
<Display @bind-Value="@Model2.Name"></Display>
<BootstrapInputGroupLabel DisplayText="Test" />
</BootstrapInputGroup>
</div>
<div class="col-12 col-sm-6">
<BootstrapInputNumber @bind-Value="@Model2.Count" />
</div>
<div class="col-12 col-sm-6">
<DateTimePicker @bind-Value="@Model2.DateTime" />
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="@Model2.Education" />
</div>
<div class="col-12">
<CheckboxList @bind-Value="@Model2.Hobby" Items="@Hobbies2" />
</div>
<div class="col-12">
<Button ButtonType="@ButtonType.Submit" Icon="fa-fw fa-solid fa-floppy-disk" IsAsync="true" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
</div>
</div>
</ValidateForm>
<ConsoleLogger @ref="Logger2" />
</DemoBlock>

<DemoBlock Title="@Localizer["ValidateFormCustomDisplayErrorTitle"]" Introduction="@Localizer["ValidateFormCustomDisplayErrorIntro"]" Name="CustomDisplayError">
<section ignore>@((MarkupString)Localizer["ValidateFormCustomDisplayErrorDescription"].Value)</section>
<ValidateForm @ref="FooForm" Model="@Model3" OnValidSubmit="@OnValidSetError">
<div class="row g-3">
<div class="col-12 col-sm-6">
<BootstrapInput @bind-Value="@Model3.Name" />
</div>
<div class="col-12 col-sm-6">
<BootstrapInputNumber @bind-Value="@Model3.Count" />
</div>
<div class="col-12 col-sm-6">
<DateTimePicker @bind-Value="@Model3.DateTime" />
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="@Model3.Education" />
</div>
<div class="col-12">
<CheckboxList @bind-Value="@Model3.Hobby" Items="@Hobbies3" />
</div>
<div class="col-12">
<Button ButtonType="@ButtonType.Submit" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
</div>
</div>
</ValidateForm>
</DemoBlock>
<DemoBlock Title="@Localizer["ValidateFormCustomDisplayErrorTitle"]" Introduction="@Localizer["ValidateFormCustomDisplayErrorIntro"]" Name="CustomDisplayError">
<section ignore>@((MarkupString)Localizer["ValidateFormCustomDisplayErrorDescription"].Value)</section>
<ValidateForm @ref="FooForm" Model="@Model3" OnValidSubmit="@OnValidSetError">
<div class="row g-3">
<div class="col-12 col-sm-6">
<BootstrapInput @bind-Value="@Model3.Name" />
</div>
<div class="col-12 col-sm-6">
<BootstrapInputNumber @bind-Value="@Model3.Count" />
</div>
<div class="col-12 col-sm-6">
<DateTimePicker @bind-Value="@Model3.DateTime" />
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="@Model3.Education" />
</div>
<div class="col-12">
<CheckboxList @bind-Value="@Model3.Hobby" Items="@Hobbies3" />
</div>
<div class="col-12">
<Button ButtonType="@ButtonType.Submit" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
</div>
</div>
</ValidateForm>
</DemoBlock>

<DemoBlock Title="@Localizer["ValidateFormValidatorAllFieldTitle"]" Introduction="@Localizer["ValidateFormValidatorAllFieldIntro"]" Name="ValidatorAllField">
<section ignore>@((MarkupString)Localizer["ValidateFormValidatorAllFieldDescription"].Value)</section>
<ValidateForm Model="@Model4" OnInvalidSubmit="@OnInvalidSubmitAddress"
ValidateAllProperties="true">
<div class="row g-3">
<div class="col-12 col-sm-6">
<BootstrapInput @bind-Value="@Model4.Name" />
</div>
<div class="col-12 col-sm-6">
<BootstrapInputNumber @bind-Value="@Model4.Count" />
</div>
<div class="col-12 col-sm-6">
<DateTimePicker @bind-Value="@Model4.DateTime" />
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="@Model4.Education" />
</div>
<div class="col-12">
<CheckboxList @bind-Value="@Model4.Hobby" Items="@Hobbies4" />
</div>
<div class="col-12">
<Button ButtonType="@ButtonType.Submit" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
</div>
</div>
</ValidateForm>
<ConsoleLogger @ref="Logger4" />
</DemoBlock>
<DemoBlock Title="@Localizer["ValidateFormValidatorAllFieldTitle"]" Introduction="@Localizer["ValidateFormValidatorAllFieldIntro"]" Name="ValidatorAllField">
<section ignore>@((MarkupString)Localizer["ValidateFormValidatorAllFieldDescription"].Value)</section>
<ValidateForm Model="@Model4" OnInvalidSubmit="@OnInvalidSubmitAddress"
ValidateAllProperties="true">
<div class="row g-3">
<div class="col-12 col-sm-6">
<BootstrapInput @bind-Value="@Model4.Name" />
</div>
<div class="col-12 col-sm-6">
<BootstrapInputNumber @bind-Value="@Model4.Count" />
</div>
<div class="col-12 col-sm-6">
<DateTimePicker @bind-Value="@Model4.DateTime" />
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="@Model4.Education" />
</div>
<div class="col-12">
<CheckboxList @bind-Value="@Model4.Hobby" Items="@Hobbies4" />
</div>
<div class="col-12">
<Button ButtonType="@ButtonType.Submit" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
</div>
</div>
</ValidateForm>
<ConsoleLogger @ref="Logger4" />
</DemoBlock>

<DemoBlock Title="@Localizer["ValidateFormComplexValidationTitle"]" Introduction="@Localizer["ValidateFormComplexValidationIntro"]" Name="ComplexValidation">
<section ignore>
<p>@((MarkupString)Localizer["ValidateFormComplexValidationDescription"].Value)</p>
<Pre class="mb-3">@Localizer["ValidateFormComplexValidationPre"]</Pre>
</section>
<DemoBlock Title="@Localizer["ValidateFormComplexValidationTitle"]" Introduction="@Localizer["ValidateFormComplexValidationIntro"]" Name="ComplexValidation">
<section ignore>
<p>@((MarkupString)Localizer["ValidateFormComplexValidationDescription"].Value)</p>
<Pre class="mb-3">@Localizer["ValidateFormComplexValidationPre"]</Pre>
</section>

<ValidateForm @ref="ComplexForm" Model="@ComplexModel" OnInvalidSubmit="@OnInvalidComplexModel"
OnValidSubmit="@OnValidComplexModel">
<div class="row g-3">
<div class="col-12 col-sm-6">
<BootstrapInput @bind-Value="@ComplexModel.Name" />
</div>
<div class="col-12 col-sm-6">
<BootstrapInput @bind-Value="@ComplexModel.Dummy.Dummy2.Name" />
</div>
<div class="col-12">
<Button ButtonType="@ButtonType.Submit" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
</div>
</div>
</ValidateForm>
<ConsoleLogger @ref="Logger5" />
<ValidateForm @ref="ComplexForm" Model="@ComplexModel" OnInvalidSubmit="@OnInvalidComplexModel"
OnValidSubmit="@OnValidComplexModel">
<div class="row g-3">
<div class="col-12 col-sm-6">
<BootstrapInput @bind-Value="@ComplexModel.Name" />
</div>
<div class="col-12 col-sm-6">
<BootstrapInput @bind-Value="@ComplexModel.Dummy.Dummy2.Name" />
</div>
<div class="col-12">
<Button ButtonType="@ButtonType.Submit" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
</div>
</div>
</ValidateForm>
<ConsoleLogger @ref="Logger5" />

<section ignore>
@foreach (var key in GetValueChangedFieldCollection())
<section ignore>
@foreach (var key in GetValueChangedFieldCollection())
{
<div>
<span>@key.Key.Model.GetType().Name</span>.<span>@key.Key.FieldName</span>: <span>@key.Value</span>
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Select/MultiSelect.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private async Task SetValue()
var validationResults = new List<ValidationResult>();

await ValidatePropertyAsync(CurrentValue, validationContext, validationResults);
ToggleMessage(validationResults, true);
ToggleMessage(validationResults);
}

if (OnSelectedItemsChanged != null)
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Transfer/Transfer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private async Task TransferItems(List<SelectedItem> source, List<SelectedItem> t
var validationResults = new List<ValidationResult>();

await ValidatePropertyAsync(RightItems, validationContext, validationResults);
ToggleMessage(validationResults, true);
ToggleMessage(validationResults);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/BootstrapBlazor/Components/Upload/InputUpload.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ private async Task OnDeleteFile()
/// <inheritdoc/>
/// </summary>
/// <param name="results"></param>
/// <param name="validProperty"></param>
public override void ToggleMessage(IEnumerable<ValidationResult> results, bool validProperty)
public override void ToggleMessage(IEnumerable<ValidationResult> results)
{
if (results.Any())
{
Expand Down
3 changes: 1 addition & 2 deletions src/BootstrapBlazor/Components/Upload/UploadBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public abstract class UploadBase<TValue> : ValidateBase<TValue>, IUpload
/// 显示/隐藏验证结果方法
/// </summary>
/// <param name="results"></param>
/// <param name="validProperty">是否对本属性进行数据验证</param>
public override void ToggleMessage(IEnumerable<ValidationResult> results, bool validProperty)
public override void ToggleMessage(IEnumerable<ValidationResult> results)
{
if (FieldIdentifier != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ public interface IValidateComponent
/// 显示或者隐藏提示信息方法
/// </summary>
/// <param name="results"></param>
/// <param name="validProperty">是否为模型验证 true 为属性验证 false 为整个模型验证</param>
void ToggleMessage(IEnumerable<ValidationResult> results, bool validProperty);
void ToggleMessage(IEnumerable<ValidationResult> results);
}
3 changes: 1 addition & 2 deletions src/BootstrapBlazor/Components/Validate/ValidateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,7 @@ private void ValidateType(ValidationContext context, List<ValidationResult> resu
/// 显示/隐藏验证结果方法
/// </summary>
/// <param name="results"></param>
/// <param name="validProperty">是否对本属性进行数据验证</param>
public virtual void ToggleMessage(IEnumerable<ValidationResult> results, bool validProperty)
public virtual void ToggleMessage(IEnumerable<ValidationResult> results)
{
if (FieldIdentifier != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void InternalSetError(MemberExpression exp, string errorMessage)
{
new(errorMessage, new string[] { fieldName })
};
validator.ToggleMessage(results, true);
validator.ToggleMessage(results);
}
}
}
Expand All @@ -181,7 +181,7 @@ public void SetError(string propertyName, string errorMessage)
{
new(errorMessage, new string[] { fieldName })
};
validator.ToggleMessage(results, true);
validator.ToggleMessage(results);
}
}

Expand Down Expand Up @@ -253,7 +253,7 @@ internal async Task ValidateObject(ValidationContext context, List<ValidationRes
await ValidateAsync(validator, propertyValidateContext, messages, pi, propertyValue);
}
// 客户端提示
validator.ToggleMessage(messages, false);
validator.ToggleMessage(messages);
results.AddRange(messages);
}
}
Expand All @@ -280,7 +280,7 @@ internal async Task ValidateFieldAsync(ValidationContext context, List<Validatio
}

// 客户端提示
validator.ToggleMessage(results, true);
validator.ToggleMessage(results);
}
}
}
Expand Down Expand Up @@ -402,7 +402,7 @@ private async Task ValidateProperty(ValidationContext context, List<ValidationRe
await ValidateAsync(validator, context, messages, pi, propertyValue);

// 客户端提示
validator.ToggleMessage(messages, true);
validator.ToggleMessage(messages);
}
results.AddRange(messages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Extensions/JSModuleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static async Task<JSModule> LoadModule(this IJSRuntime jsRuntime, string
var jSObjectReference = await jsRuntime.InvokeAsync<IJSObjectReference>(identifier: "import", fileName);
module = new JSModule(jSObjectReference);
}
catch (OperationCanceledException) { }
catch (Exception) { }
return module ?? new JSModule(null);
}

Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Components/UploadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public void ButtonUpload_ValidateForm_Ok()
{
new("test", memberNames)
};
uploader.Instance.ToggleMessage(results, true);
uploader.Instance.ToggleMessage(results);
}

[Fact]
Expand Down