具有Blazor EditForm
和包含的InputTextArea
(即多行文本框),当用户按下 Ctrl +时,我确实想验证并提交表单输入,就像他单击提交按钮一样。
我已经成功连接了键盘处理器,如下所示:
<EditForm Model="@myModel" Format="g" OnValidSubmit="@Store" @ref="_editForm">
<InputTextArea
onkeypress="@(async e => await myKeyPress(e))"
@bind-Value="myModel.Foo" />
<button type="submit">Store it</button>
</EditForm>
后面有此代码:
private EditForm _editForm;
private async Task myKeyPress(KeyboardEventArgs key)
{
if (key.CtrlKey && key.Code == @"Enter")
{
_editForm.??? // What to call here?
}
}
不幸的是,我在EditForm类中没有看到可以调用以提交和验证表单的方法,就像用户单击提交按钮一样。
如何以编程方式提交和验证Blazor表单?
答案 0 :(得分:3)
<EditForm Context=MyCurrentEditContext>
<InputTextArea
onkeypress="@(async e => await myKeyPress(MyCurrentEditContext, e))"
@bind-Value="myModel.Foo" />
<button type="submit">Store it</button>
</EditForm>
@code
{
private async Task myKeyPress(EditContext editContext, KeyboardEventArgs key)
{
if (key.CtrlKey && key.Code == @"Enter")
{
if (editContext.Validate())
{
... Do stuff if valid
}
}
}
}