Skip to content

fix(ValidateForm): DisableAutoSubmitFormByEnter not work in Modal - #8282

Merged
ArgoZhang merged 6 commits into
mainfrom
dev-form
Jul 31, 2026
Merged

fix(ValidateForm): DisableAutoSubmitFormByEnter not work in Modal#8282
ArgoZhang merged 6 commits into
mainfrom
dev-form

Conversation

@ArgoZhang

@ArgoZhang ArgoZhang commented Jul 31, 2026

Copy link
Copy Markdown
Member

Link issues

fixes #8281

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Ensure ValidateForm correctly disables Enter key auto-submit, including when used in modal or dynamically re-rendered forms.

Bug Fixes:

  • Fix DisableAutoSubmitFormByEnter so the Enter key no longer submits the form when validation is hosted in a modal or other non-standard form layouts.

Enhancements:

  • Track ValidateForm DOM elements and rebind keydown handlers on re-render to keep client-side behavior in sync with the component lifecycle.

@bb-auto bb-auto Bot added the bug Something isn't working label Jul 31, 2026
@sourcery-ai

sourcery-ai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adjusts ValidateForm’s JavaScript and Blazor logic so DisableAutoSubmitFormByEnter correctly prevents Enter-key form submission, including in modals and when the form DOM element changes, by tracking instances and rebinding handlers as needed.

Sequence diagram for ValidateForm Enter-key handling and DOM updates

sequenceDiagram
    participant ValidateForm
    participant JSRuntime
    participant JSModule as ValidateForm_razor_js
    participant DataStore as Data
    participant Dom as FormElement

    ValidateForm->>JSRuntime: InvokeVoidAsync(update, Id)
    JSRuntime->>JSModule: update(id)
    JSModule->>Dom: document.getElementById(id)
    JSModule->>DataStore: Data.get(id)
    alt [form element changed]
        JSModule->>JSModule: dispose(id)
        JSModule->>DataStore: Data.remove(id)
        JSModule->>JSModule: unbind(form.el)
        JSModule->>JSModule: init(id)
        JSModule->>Dom: document.getElementById(id)
        JSModule->>DataStore: Data.set(id, { el })
        JSModule->>JSModule: bind(el)
        JSModule->>Dom: EventHandler.on(keydown)
    else [form element unchanged]
        JSModule-->>ValidateForm: no changes
    end
Loading

File-Level Changes

Change Details Files
Refactor ValidateForm JavaScript to track form instances and bind/unbind Enter-key suppression only when auto-submit is disabled, using the modern KeyboardEvent key property.
  • Import shared Data module to store per-form state keyed by element id.
  • Store the form element in Data on init and only bind the keydown handler when data-bb-dissubmit is true.
  • Add an update(id) function that detects when the form element has changed, disposes old bindings, and re-initializes for the new element.
  • Change dispose(id) to read/remove form state from Data and unbind the keydown handler via a dedicated unbind helper.
  • Switch key detection from e.keyCode === 13 to e.key === 'Enter' and ensure suppression only when the target is not a TEXTAREA.
src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js
Update ValidateForm Blazor component to only emit the DisableAutoSubmit attribute for real forms and to notify JS when the rendered element changes.
  • Change DisableAutoSubmitString to return null when IsFormless is true, preventing data-bb-dissubmit from being rendered for formless usage such as modals.
  • Override OnAfterRenderAsync to call the new JavaScript update(id) method on subsequent renders whenever the component re-renders.
  • Ensure base.OnAfterRenderAsync is called before invoking the JS update helper.
src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#8281 Ensure DisableAutoSubmitFormByEnter correctly prevents Enter-key auto-submit in ValidateForm, including when used inside a Modal (where the form element can change after render).
#8281 Wire the DisableAutoSubmitFormByEnter behavior to the DOM lifecycle so event handlers are correctly bound/unbound when the ValidateForm is updated or disposed (e.g., due to Modal re-rendering).

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot added this to the v10.8.0 milestone Jul 31, 2026

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In ValidateForm.razor.js's update function, const form = Data.get(id); if (el === form.el) will throw if no data has been stored for that id (e.g., when init was never called), so add a null/undefined guard around form before accessing form.el.
  • Both init and update assume document.getElementById(id) returns a valid element; consider returning early if el is null to avoid passing null into Data.set/EventHandler.on when the element is not present in the DOM (for example, in formless scenarios or after removal).
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `ValidateForm.razor.js`'s `update` function, `const form = Data.get(id); if (el === form.el)` will throw if no data has been stored for that id (e.g., when `init` was never called), so add a null/undefined guard around `form` before accessing `form.el`.
- Both `init` and `update` assume `document.getElementById(id)` returns a valid element; consider returning early if `el` is null to avoid passing `null` into `Data.set`/`EventHandler.on` when the element is not present in the DOM (for example, in formless scenarios or after removal).

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js" line_range="18-20" />
<code_context>
+
+export function update(id) {
+    const el = document.getElementById(id);
+    const form = Data.get(id);
+
+    if (el === form.el) {
+        return;
+    }
</code_context>
<issue_to_address>
**issue (bug_risk):** Handle missing Data entry in update() to avoid runtime errors.

If `update` runs before `init` stores the element, or after `Data.remove`, `Data.get(id)` can be `undefined`, causing `form.el` to throw. Add a null check and return early when `form` is falsy before accessing `form.el`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js Outdated
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (cd6bb35) to head (14cf3da).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #8282   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          769       769           
  Lines        34436     34445    +9     
=========================================
+ Hits         34436     34445    +9     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@ArgoZhang
ArgoZhang merged commit 586354f into main Jul 31, 2026
7 checks passed
@ArgoZhang
ArgoZhang deleted the dev-form branch July 31, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(ValidateForm): DisableAutoSubmitFormByEnter not work in Modal

1 participant