Fix mask resurrecting key in object when it is cleared#4812
Open
danharrin wants to merge 1 commit intoalpinejs:mainfrom
Open
Fix mask resurrecting key in object when it is cleared#4812danharrin wants to merge 1 commit intoalpinejs:mainfrom
danharrin wants to merge 1 commit intoalpinejs:mainfrom
Conversation
Contributor
|
Why is this the way you want to clear the form state? Can you not use |
Contributor
Author
|
This is not for clearing form state, this is part of a larger system that manages nested modal stacks that can contain forms. |
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.
When a reactive object is cleared (e.g.
form = {}), Alpine's x-model effect fires_x_forceModelUpdate(undefined)on inputs still connected to the DOM. The mask plugin's wrapper around this function convertsundefinedto the string"undefined"viaString(value), formats it to"", then callsel._x_model.set('')— which evaluatesform.amount = '', resurrecting the deleted key with an empty value. In a Livewire context, this stale key gets sent back with the next server request, corrupting server-side state.Steps to reproduce:
x-model="form.amount"andx-mask:dynamic="$money($input)"form.amountto a value (e.g.'5000')form = {}form.amountreappears as an empty stringFindings
The mask plugin wraps
_x_forceModelUpdate(defined in x-model.js) to apply formatting before updating the DOM. The original function already handlesundefinedcorrectly for nested model paths (converts to''and updates the input). The issue is that the mask wrapper runsString(value)andel._x_model.set(value)unconditionally — even when the value isundefinedbecause the model path no longer exists.Proposed solution
Add an early return in the mask wrapper when
value === undefined. This skips the formatting/write-back path entirely and delegates to the original_x_forceModelUpdate, which already handlesundefinedproperly. Crucially,el._x_model.set()is not called, so the deleted key is not recreated.