我正在使用 MudBlazor,我想要在对话框中使用 fluentvalidation (EditForm) 的验证表单。
BookDialog.razor
<MudDialog>
<DialogContent>
<EditForm Model="@model">
<FluentValidationValidator />
<MudTextField Label="@L["Code"]" @bind-Value="model.Code" For="() => model.Code" />
<MudTextField Label="@L["Title"]" @bind-Value="model.Title" For="() => model.Title" />
</EditForm>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="MudBlazor.Color.Primary" OnClick="Submit">Ok</MudButton>
</DialogActions>
</MudDialog>
@code{
[Parameter]
public BookInfo model { get; set; }
[CascadingParameter] MudDialogInstance MudDialog { get; set; }
private void Cancel()
{
MudDialog.Cancel();
}
private void Submit()
{
MudDialog.Close(DialogResult.Ok<BookInfo>(model));
}
}
索引剃刀
@inject IDialogService dialogService
<MudIconButton Icon="@Icons.Material.Filled.Add" OnClick="CreateBook"></MudIconButton>
@code{
private async Task CreateBook()
{
var parameters = new dialogParameters();
parameters.Add("model", new BookInfo());
var dialog = await dialogService.Show<BookDialog>("Create A Book", parameters).Result;
if (dialog.Data != null)
{
//....
}
}
}
BookValidator.cs
public class BookValidator : AbstractValidator<BookInfo>
{
public BookValidator()
{
CascadeMode = CascadeMode.Stop;
RuleFor(x => x.Code).NotEmpty().WithMessage("*");
RuleFor(x => x.Title).NotEmpty().WithMessage("*");
}
}
错误消息正确显示,但关闭对话框并且不停止表单提交。