9

Having a Blazor EditForm and a contained InputTextArea (i.e. a multiline text box), I do want to validate and submit the form, when the user presses Ctrl+Enter, just as if he would click the submit button.

I've successfully got the keyboard handler connected like this:

<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>

With this code behind:

private EditForm _editForm;

private async Task myKeyPress(KeyboardEventArgs key)
{
    if (key.CtrlKey && key.Code == @"Enter")
    {
        _editForm.??? // What to call here?
    }
}

Unfortunately, I see no method in the EditForm class that I could call to submit and validate the form, as if the user would click the submit button.

I've looked at this and this SO question with no success.

My question

How to programmatically submit and validate a Blazor form?

3
  • But suppose that the user has only pressed Enter after typing. This will also submit the form... Actually, the form always submits if an input control has the focus, and you hit enter. Think about that, and reformulate your question... Commented Jun 13, 2020 at 14:05
  • 2
    @enet Since it is an InputTextArea which ist a multiline control, pressing the Enter key simply puts the keyboard caret into the next line. Commented Jun 13, 2020 at 15:02
  • Yes, you're right. I did not put attention to this fact. Commented Jun 13, 2020 at 16:12

1 Answer 1

7
<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
        }
    }
}
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot, Peter. This nearly did the trick. Unfortunately, the InputTextArea has a validation on it that it must be non-empty. When this field has the input focus and the keyboard handler fires, the exchange between the textarea and the bound model field is not yet performed. I have to change the input focus forth and back first. I even tried callingNotifyFieldChanged without success. Any hint on whether/how to fill the model value even without a focus change?
OK, I solved it by deriving my own InputTextArea (like e.g. here) and adding an oninput handler, in addition to the onchange handler. In fact, I copied the code from the original source and added the oninput handler. With this, the change is triggered not only when the focus is lost, but immediately.
@UweKeim Try adding @bind-Value:event="oninput" to your InputText. Not certain it will work, but it might.
Thanks, @PeterMorris Your help here and your tons of tutorials and examples on your website helped and help me in an awesome lot of cases. Thank you very much for your efforts!
@UweKeim Did it work on <InputText> then? I thought it should, but I've never tried.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.