【发布时间】:2021-09-17 19:49:24
【问题描述】:
我在自定义验证器中创建了以下规则:
public class AddInvoiceParameterCommandValidator : AbstractValidator<AddInvoiceParameterCommand>
{
public AddInvoiceParameterCommandValidator(IStringLocalizer<AddInvoiceParameterCommandValidator> localizer)
{
RuleFor(p => p.Start)
.Must(x => x > 0)
.WithMessage(x => localizer["Start must be greater than 0!"]);
RuleFor(p => p.Prefix)
.Must(x => !string.IsNullOrWhiteSpace(x))
.WithMessage(x => localizer["Prefix is required!"])
.MaximumLength(5)
.WithMessage(x => localizer["Length must not exceed"] + " 5 characters");
RuleFor(p => p.End)
.Must(x => x > 0)
.WithMessage(x => localizer["End must be greater than 0!"]);
RuleFor(x => x)
.Must(StartMustBeLessThanEnd)
.WithMessage(localizer["Start must be less than end!"]);
}
private bool StartMustBeLessThanEnd(AddInvoiceParameterCommand parameter)
{
return parameter.Start < parameter.End;
}
}
我对 Start 和 End 没有问题,因为我在前端表单中收到消息,当它们不大于 0 时。 问题是,对于“开始必须小于结束”规则,如何在前端显示消息?
这是我当前使用的模态代码:(Blazor WASM + MudBlazor)
@inject Microsoft.Extensions.Localization.IStringLocalizer<AddInvoiceParameterModal> _localizer
<EditForm Model="@AddInvoiceParameterModel" OnValidSubmit="SaveAsync">
<FluentValidationValidator @ref="_fluentValidationValidator" />
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-3 mb-n1" />
@_localizer["Add Parameter"]
</MudText>
</TitleContent>
<DialogContent>
<MudGrid>
<MudItem xs="12" md="6">
<MudTextField T="int" For="@(() => AddInvoiceParameterModel.Start)" @bind-Value="AddInvoiceParameterModel.Start" Label="@_localizer["Start"]" />
</MudItem>
<MudItem xs="12" md="6">
<MudTextField T="int" For="@(() => AddInvoiceParameterModel.End)" @bind-Value="AddInvoiceParameterModel.End" Label="@_localizer["End"]" />
</MudItem>
<MudItem xs="12" md="6">
<MudTextField T="string" For="@(() => AddInvoiceParameterModel.Prefix)" @bind-Value="AddInvoiceParameterModel.Prefix" Label="@_localizer["Prefix"]" />
</MudItem>
</MudGrid>
</DialogContent>
<DialogActions>
<MudButton DisableElevation Variant="Variant.Filled" OnClick="Cancel">@_localizer["Cancel"]</MudButton>
<MudButton DisableElevation Variant="Variant.Filled" ButtonType="ButtonType.Submit" Disabled="@(!Validated)" Color="Color.Success">@_localizer["Save"]</MudButton>
</DialogActions>
</MudDialog>
</EditForm>
【问题讨论】:
标签: c# .net frontend blazor fluentvalidation