Replies: 8 comments 7 replies
-
not a good idea with that. it's better to let hook set the isDirty flag for you, because it will likely to be reverted back, because we will do a deepEqual with your defaultValues. |
Beta Was this translation helpful? Give feedback.
-
So, how to set the formState to be isDirty = true? I even tried to add a defaultValues to the useForm, but that also didn't help. |
Beta Was this translation helpful? Give feedback.
-
This may help #72 (comment) |
Beta Was this translation helpful? Give feedback.
-
Its not working with useformContext().
|
Beta Was this translation helpful? Give feedback.
-
Is there a way to set multiple value at once using setValue? |
Beta Was this translation helpful? Give feedback.
-
if you set an object, it's values will not be set as // only a will be marked as dirty, b and c will not
methods.setValue("a", {b: "", c: 0}, {
shouldDirty: true,
});
// d will be dirty
methods.setValue("d", "", {
shouldDirty: true,
});
// now b will be dirty
methods.setValue("a.b", "", {
shouldDirty: true,
});``` |
Beta Was this translation helpful? Give feedback.
-
This worked for me |
Beta Was this translation helpful? Give feedback.
-
This needs to be revisited. Tried all of the approaches suggested and it's still not dirtying as expected. I understand the issue with react render cycles and why only fields would be set to dirty, but if a nested field is set to dirty, it does not make sense to me that the form itself is not set to dirty as well. In our use case, we have a list of objects nested inside an object nested inside of an object. Ie the path would be "field.subfield.[0].anotherSubfield". Manually setting the entire parent chain up to the root does not dirty any of the fields or subfields. We found a workaround by passing both shouldDirty: true and shouldTouch: true in this unique case to the subfield and all the way up the chain to parent field at the root of the form data object. We are no longer relying on formState.isDirty and instead are scanning the formData manually and treating the union of isDirty and isTouched as our 'dirty' state. We run this scan in the submit handler of our form. This may be suboptimal but it was the only way we were able to work around what seems to be an inconsistency of what defines a dirty form. I've read the docs over and over and am unable to reconcile the definition there with some of the other discussions / issues raised. Maybe this should be a feature request: expose the ability to mutate formState.isDirty to true via useFormContext. This essentially what we need. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I need the form to be dirty when a custom component has been interacted with.
The fastest way for me to do that is to set
form.formState.isDirty = true.
Is this something i can do? or not recommended based on best practice?
I'm having issues with using setValue to trigger formState.isDirty as true,
setValue does not set it as true until after the third interaction with the component.
Beta Was this translation helpful? Give feedback.
All reactions