final-form / final-form

🏁 Framework agnostic, high performance, subscription-based form state management

Home Page:https://final-form.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why not reset all fields' state when call form.reset()?

zation opened this issue · comments

I saw the code in reset only clean all the form state, not the field state. If there is any required field and the initial field value is blank, the error will show up.
Why not to reset all fields state in form.reset?

I've encountered the same issue

I have encountered the same issue. Especially in a post form submission, it makes the form behave as if an error had occurred. Currently, the only solution I have found is to also resetFieldState for each field, and it gets more complicated. Reset should reset everything in the form and fields.

<form
	onSubmit={async (event) => {
	await handleSubmit(event);
	form.resetFieldState('titleEN');
	form.resetFieldState('titleEN');
	form.reset();
}}>

I figured I'd open the MR since this is a quite common use-case, although, it should be noted, this is already possible through the current API.

https://github.com/final-form/final-form/pull/348/files#diff-1fbdeac5e3837fb021ccc625fb2d8442R965

Should that MR not be accepted, you may just copy the code and put it in a helper function.

  1. Iterate through all the fields from form.getState(); call form.resetFieldState on each
  2. Call form.reset()

Published fix in v4.20.0.