Replies: 3 comments 10 replies
-
You have total control over what your onSubmit handler on the form actually does, right? Why not just have your own logic compare the submitted values to what was, for example, specified in defaultValues to the useForm hook? Why should that be up to a form library? |
Beta Was this translation helpful? Give feedback.
1 reply
-
Thanks for the idea, Maybe we can export a function (not within the main library) to gather updated field values, and keep the main library light and small. function getDirtyFields({ formState: { dirtyFields }, getValues}) {
// with dirtyFields and getValues() we can return updated fields values
}
const hookFormMethods = useForm();
onSubmit={() => {
const updatedFields = getDirtyFields(hookFormMethods)
}}
onClick={() => {
const updatedFields = getDirtyFields(hookFormMethods)
}} It would be quite flexible as you can use it even with click event without validation, eg save for later. |
Beta Was this translation helpful? Give feedback.
9 replies
-
This is how I do const onSubmit: SubmitHandler<IType> = async (data: IType) => {
const _data: IType= {} as IType;
Object.keys(dirtyFields).map((key) => {
_data[key] = data[key as keyof IType];
});
if (!Object.keys(_data).length) return // no changes
// if change...
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Originally discussed 2 years ago: #1991
Our backend processes updates much faster if it only needs to update necessary fields. I'd like a first-class way to only submit fields that are dirty. Or, a well-documented approach for this. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions