You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The useForm helper’s isDirty property shows if any field has changed, but there’s no built-in way to check which fields are dirty without extra code. This is a frequent need for form UIs (e.g., marking changed fields).
Solution
Add a dirtyFields property to useForm, a reactive object that tracks which fields have changed. Example:
This builds on the existing initial-value tracking in useForm, exposing per-field states reactively (e.g., via computed in Vue). It’s inspired by React Hook Form’s dirtyFieldshttps://react-hook-form.com/docs/useformstate and relates to prior discussions in PR #1387, where form state enhancements were explored.
This is just to demo the idea via a quick example; it does not implement deep field checking like getDirtyFields.test.ts. Starting small and allowing checks at the root level only at first seems like an expandable approach. If deep checks were introduced later, it would not cause backward compatibility issues. [example deep check here]
watch(form,(newValue)=>{constcurrentData=form.data()form.isDirty=!isEqual(currentData,defaults)// Update dirtyFieldsObject.keys(defaults).forEach((key)=>{form.dirtyFields[key]=!isEqual(currentData[key],defaults[key])})// Clean up dirtyFields for removed keysObject.keys(form.dirtyFields).forEach((key)=>{if(!(keyindefaults))deleteform.dirtyFields[key]})if(rememberKey){router.remember(cloneDeep(newValue.__remember()),rememberKey)}},{immediate: true,deep: true},)
Like isDirty (for onSuccess(), const form = reactive({}), and other place), we can set the default to an empty object {} to standardize the clean value regardless of the form data set. So, when this.isDirty = false to reset it, we set this.dirtyFields = {}.
this.isDirty=false;this.dirtyFields={};
Finally, we might need to follow the pattern of errors when a restore or remember happens. I'm not sure if we should ignore them or not.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Problem
The
useForm
helper’sisDirty
property shows if any field has changed, but there’s no built-in way to check which fields are dirty without extra code. This is a frequent need for form UIs (e.g., marking changed fields).Solution
Add a
dirtyFields
property touseForm
, a reactive object that tracks which fields have changed. Example:This builds on the existing initial-value tracking in
useForm
, exposing per-field states reactively (e.g., via computed in Vue). It’s inspired by React Hook Form’sdirtyFields
https://react-hook-form.com/docs/useformstate and relates to prior discussions in PR #1387, where form state enhancements were explored.Implementation
Current Implementation (Vue)
New Implementation (Vue)
This is just to demo the idea via a quick example; it does not implement deep field checking like getDirtyFields.test.ts. Starting small and allowing checks at the root level only at first seems like an expandable approach. If deep checks were introduced later, it would not cause backward compatibility issues. [example deep check here]
Like
isDirty
(foronSuccess()
,const form = reactive({})
, and other place), we can set the default to an empty object{}
to standardize the clean value regardless of the form data set. So, whenthis.isDirty = false
to reset it, we setthis.dirtyFields = {}
.Finally, we might need to follow the pattern of errors when a
restore
orremember
happens. I'm not sure if we should ignore them or not.Why?
useForm
(see AddsclearChangedErrors
method to clear changed fields errors #1387, Make isDirty reactive to defaults #2236).Beta Was this translation helpful? Give feedback.
All reactions