[英]Blazor EditForm and Fluent Validation
Just putting together a wrapper based on EditForm and using Fluent Validation.只需将基于 EditForm 并使用 Fluent Validation 的包装器放在一起。 I've created two properties on this form as below:
我在此表单上创建了两个属性,如下所示:
if (ModelValidation)
{
editContext.OnValidationRequested +=
(sender, eventArgs) => ValidateModel((EditContext)sender, messages);
}
if (FieldValidation)
{
editContext.OnFieldChanged +=
(sender, eventArgs) => ValidateField(editContext, messages, eventArgs.FieldIdentifier);
}
This allows validation to be either on OnFieldChanged (value changes, validated on exit field) or when a submit button is pressed (OnValidationRequested)这允许验证在 OnFieldChanged 上(值更改,在退出字段上验证)或在按下提交按钮时(OnValidationRequested)
However, if I have say a text field which is empty (which should be non-empty) tab out of it the OnFieldChanged() handler does not fire...(not surprising the field hasn't changed).但是,如果我说一个为空的文本字段(应该是非空的)选项卡,则 OnFieldChanged() 处理程序不会触发......(这并不奇怪该字段没有改变)。 Is there a way of forcing the call to OnFieldChanged() or a kill focus handler without resorting to javascript?
有没有办法在不诉诸 javascript 的情况下强制调用 OnFieldChanged() 或终止焦点处理程序?
I thought this was interesting so I took a look at the source code of Blazor's InputText
class on the ASP .NET Core project of GitHub here:我认为这很有趣,所以我在 GitHub 的 ASP .NET 核心项目上查看了 Blazor 的
InputText
class 的源代码:
https://github.com/dotnet/aspnetcore/blob/master/src/Components/Web/src/Forms/InputText.cs https://github.com/dotnet/aspnetcore/blob/master/src/Components/Web/src/Forms/InputText.cs
You'll notice that there is only one event handler and it's for the onchange
event raised by the browser when the user changes the value in the text box.您会注意到只有一个事件处理程序,它用于当用户更改文本框中的值时浏览器引发的
onchange
事件。
builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder<string>(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
The event you want to subscribe to is onblur
, which is raised when the user clicks or tabs out of a field regardless of whether they changed it or not, and it's not been done.您要订阅的事件是
onblur
,无论用户是否更改了字段,当用户单击或跳出字段时都会引发该事件,并且尚未完成。
I couldn't find it anywhere in InputText or InputBase (from which InputText derives) but it's there somewhere because this seems to work:我在 InputText 或 InputBase (从 InputText 派生)的任何地方都找不到它,但它在某处,因为这似乎有效:
@page "/"
<InputText @onblur="DoSomething" />
@code
{
private void DoSomething()
{ // Your logic here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.