Disable Form Submit when pressing Enter after combobox edit

Hello!

I have a Blazor combobox that is placed inside a EditForm element, like this:

<EditForm class="p-1 border bg-light" Model="SelectedSetup" OnValidSubmit="HandleValidSubmit">


                <div class="control-wrapper mb-4">

                    <label for="lstDMS" class="form-label mb-0">Select or Enter a Run Tag/Code to Use:</label>

                    <SfComboBox TItem="TriggerModelDTO" TValue="string" DataSource="@Setups" PopupHeight="230px" Placeholder="Enter or Select a Run Tag or Code"

                          @bind-Value="@SelectedSetup.SetupName" AllowCustom="true">

                    <ComboBoxEvents TItem="TriggerModelDTO" TValue="string" ValueChange="@OnChangeSetupNameHandler"></ComboBoxEvents>

                     <ComboBoxFieldSettings Text="SetupName" Value="SetupName"/>

                    </SfComboBox>

                </div>

</EditForm>


As you can see above, I want the user to be able to be able to add new values to the list but setting AllowCustom = "true". But the problem is that, after the user types in the new text, they will typically press the Enter button, which will cause the container EditForm to Submit.

How can I avoid this from happening, so that the user can press Enter after editing the combobox value without the form submit being triggered?

Many thanks!



3 Replies

PM Ponmani Murugaiyan Syncfusion Team March 1, 2022 06:56 AM UTC

Hi Fritz, 

You can prevent the ENTER key in the EditForm submitting using JS Interop as like below code snippet. 

[Index.razor]

 
<div id="wrapper"> 
    <EditForm id="askQuestionForm" Model="@_testModel" OnValidSubmit="HandleSubmit"> 
        <DataAnnotationsValidator /> 
        <ValidationSummary /> 
        <SfComboBox TValue="string" TItem="GameFields" AllowCustom="true" PopupHeight="230px" Placeholder="Select a game" @bind-Value="_testModel.Value" DataSource="@Games"> 
            <ComboBoxFieldSettings Text="Text" Value="ID" /> 
        </SfComboBox> 
        <button type="submit">Submit</button> 
    </EditForm> 
</div> 
@code { 
    private Test _testModel = new Test(); 
 
    private void HandleSubmit() 
    { 
        Console.WriteLine("Submit..."); 
    } 
    protected override void OnAfterRender(bool firstRender) 
    { 
        if (firstRender) 
        { 
           JS.InvokeVoidAsync("PreventEnterKey", "askQuestionForm"); 
        } 
    } 
    public class Test 
    { 
        [Required] 
        public string Value { get; set; } 
    } 
 
    ... 
} 

[wwwroot/calendar.js] 
 
window.PreventEnterKey = (id) => { 
    var formObj = document.getElementById(id); 
    formObj.addEventListener('keydown', function (event) { 
        if (event.keyCode == 13) { 
            event.preventDefault(); 
            return false; 
        } 
    }) 
}; 


Regards, 
Ponmani M 



FR Fritz replied to Ponmani Murugaiyan March 12, 2022 08:16 AM UTC

Many thanks Ponmani for your kind response. The sample project showed me how to solve the problem. Thanks again.



PM Ponmani Murugaiyan Syncfusion Team March 14, 2022 04:56 AM UTC

Hi Fritz, 

Welcome, we are glad to hear that your issue has been solved. 

Regards, 
Ponmani M 


Loader.
Up arrow icon