rvf icon indicating copy to clipboard operation
rvf copied to clipboard

[Feature]: dirtyFields & isDirty

Open guvenakcoban opened this issue 3 years ago • 5 comments

What is the new or updated feature that you are suggesting?

dirtyFields and isDirty states like react-hook-form

Why should this feature be included?

It would be really useful If we can get isDirty state to use disabling submit button and/or we can post only dirty fields.

guvenakcoban avatar Apr 30 '22 12:04 guvenakcoban

This is something I want to add, but not sure I can promise a timeline right now.

airjp73 avatar May 01 '22 03:05 airjp73

It would be really useful to have this! I'm aware too!

ThomiReynoso avatar Aug 25 '22 17:08 ThomiReynoso

Seconding this, it's necessary to know if the form is dirty to show reset button when dirty and disable submit button when not dirty.

alexbu92 avatar Sep 19 '22 13:09 alexbu92

Would be awesome to have this!

It would be very useful for building an You have unsaved changes dialog when having a dirty form on a route change.

hauptrolle avatar Oct 06 '22 12:10 hauptrolle

Not sure how much time I have to implement this, so I want to leave a workaround for anyone struggling with this.

You can handle most use-cases for this to some degree without an explicit dirtyFields state. You can add listen to change events for the whole form by adding a single change listener higher in the DOM. Here's an example:

const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
return (
  <ValidatedForm validator={validator} onChange={() => setHasUnsavedChanges(true)}>
    {/* etc */}
  </ValidatedForm>
)

If you have inputs outside the form itself, you could attach the onChange to any element like a div.

const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
return (
  <div onChange={() => setHasUnsavedChanges(true)}>
    <ValidatedForm validator={validator}>
      {/* etc */}
    </ValidatedForm>
  </div>
)

This won't handle 100% of cases, like if you have a custom component that only works through useControlField, but it should work for most people.

airjp73 avatar Oct 27 '22 03:10 airjp73