Flush x-model.blur value before form submit handlers run#4729
Merged
calebporzio merged 1 commit intomainfrom Feb 2, 2026
Merged
Flush x-model.blur value before form submit handlers run#4729calebporzio merged 1 commit intomainfrom
x-model.blur value before form submit handlers run#4729calebporzio merged 1 commit intomainfrom
Conversation
When using `x-model.blur` on an input inside a form, submitting via Enter key does not sync the value before the submit handler executes. The browser fires events in order: keydown → submit → blur, so the blur listener never runs before the submit handler reads the value. Register pending model update callbacks on the form element and flush them before any submit handler runs via the `on()` utility.
joshhanley
added a commit
to livewire/livewire
that referenced
this pull request
Jan 30, 2026
The proper fix for this belongs in Alpine's `x-model.blur` directive. See alpinejs/alpine#4729.
|
Nice! Makes sense to address it Alpine. Thanks again, @joshhanley ! |
manwithacat
added a commit
to manwithacat/alpine
that referenced
this pull request
Mar 23, 2026
When an element with x-model.blur is removed from the DOM (e.g., via x-if or Livewire morphing), the cleanup callback reads el.form which returns null for detached elements. This causes a TypeError on null._x_pendingModelUpdates. Fix: capture `let form = el.form` at registration time so the cleanup closure uses a stable reference that remains valid after detachment. Adds three Cypress tests: - x-model.blur input removed via x-if (core crash case) - x-model.blur input without form removed via x-if - form submit after sibling x-model.blur input removed Ref: alpinejs#4738 Regression from: alpinejs#4729
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Scenario
When using
x-model.bluron an input inside a form, submitting the form by pressing Enter from that input does not sync the input's value before the submit handler runs. The handler sees stale (empty) data.The Problem
The browser fires events in this order when Enter is pressed inside a form input:
keydown(Enter) on the inputsubmiton the formbluron the input (only after submit)Since
blurfires aftersubmit, Alpine'sx-model.blurlistener never syncs the value before the@submithandler executes. The.changeand.entermodifiers are not affected — the browser fireschangeandkeydownbeforesubmit.The Solution
When
x-model.bluris used on an input inside a form, a pending model update callback is registered on the form element (el.form._x_pendingModelUpdates). Theon()event utility then flushes any pending model updates before running submit handlers.x-model.js: When.bluris present and the input is inside a form, registers a sync callback on the form element and cleans it up when the directive is removedon.js: When setting up anysubmitevent handler, wraps it to flush_x_pendingModelUpdateson the form before the handler executes