Fix typo in form key that uses $persist #4041
Replies: 2 comments 2 replies
-
Just on init, look at the data and check for the old one and change it to the new one... Also, should probably have a default value, not just undefined... |
Beta Was this translation helpful? Give feedback.
-
Hi @ekwoka – thanks for the reply. I’m not sure I understand your solution. Here’s my component, stripped down version: const form = () => {
return {
formData: this.$persist({
street: '',
City: '',
country: '',
}).using(cookieStorage)
}
} To fix the API call, I need it to be this, notice lowercase const form = () => {
return {
formData: this.$persist({
street: '',
city: '',
country: '',
}).using(cookieStorage)
}
} The whole <input type="text" id="City" name="City" x-model="formData['City']">
<!-- Changed to: -->
<input type="text" id="city" name="city" x-model="formData['city']"> Only changing the code like above results in the input showing pre-filled with the word Do I understand correctly that you would suggest this? init() {
if (this.orderData.City) {
this.orderData.city = this.orderData.City;
delete this.orderData.City;
}
} |
Beta Was this translation helpful? Give feedback.
-
I’m using the wonderful Persist plugin to store user address information in the browser so that they don’t have to re-enter the information every time they use the form. It‘s persisted using
js-cookie
instead of localStorage. I set an expiry of 90 days.Alpine uses a property called
formData
to persist and pre-fill the form. Submitting the form sends the data to an external API. I had a typo and the city input incorrectly used the keyCity
instead ofcity
, making the API call fail.Fixing the typo was easy, but if someone has used the form already, their persisted data still uses the
City
key and the input shows "undefined" because the new key iscity
.Is there a way to fix this without the user having to manually clear the cookies?
The only way I can think of is changing the key of the
formData
property that is being persisted, so that a new cookie string is added and the old one is ”retired“ until it expires. That would mean having to change it in a couple of dozen places and my OCD itches when I have to useformData1
,formData2
etc. depending on how the API changes over time.Beta Was this translation helpful? Give feedback.
All reactions