-
Having this html element:
And this corresponding js:
Input box keeps showing any entered value even if value is not accepted by setter. I expected the input value to be reset to actual model value if new value is not accepted by setter because when I tried to bind x-model to $store instead of x-data it makes input box value reset to old value if new value is not accepted. I think it has something to do with calling this function that gets called in case of $store but not for x-data (https://github.com/alpinejs/alpine/blob/main/packages/alpinejs/src/directives/x-model.js):
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
i'm not sure that the code above is the reason. Not sure why it works with a store, in theory it shouldn't. To achieve what you need, you can use this workaround. } else {
let oldValue = this._dataInput
this._dataInput = ''
this._dataInput = oldValue
console.log('input out of range');
console.log('current value is:'+this._dataInput);
} |
Beta Was this translation helpful? Give feedback.
Okay, so it's just due to how the reactivity engine works.
Since you aren't setting the value here, when it's at the top level, the
set
isn't watched at all, but when it's a child, theset
andget
are seen and tracked even if the set doesn't do anything, so long as the value being set is different (even if it never gets added).Curious that it works that way.
Anyhow, there are better ways to handle this specific need, like a mask, or validation attributes.