Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for automatically resetting validators #907

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,13 @@
this.form.validate(cause)
} catch (_) {}

// Clear previous validation errors
if (this.form.options.automaticallyResetValidators === true)
this.setMeta((prev) => ({

Check warning on line 937 in packages/form-core/src/FieldApi.ts

View check run for this annotation

Codecov / codecov/patch

packages/form-core/src/FieldApi.ts#L937

Added line #L937 was not covered by tests
...prev,
errorMap: {},
}))

// Attempt to sync validate first
const { hasErrored } = this.validateSync(cause)

Expand Down
14 changes: 14 additions & 0 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ export interface FormOptions<
formApi: FormApi<TFormData, TFormValidator>
}) => void
transform?: FormTransform<TFormData, TFormValidator>
/**
* When true, all previous validation errors will be cleared when running validation.
*
* While `true`:
* - `onMount` and `onBlur` errors will be cleared when `onChange` triggers validation.
*
* While `false`:
* - An `onMount` error will never be cleared.
* - An `onBlur` error will only be cleared on the next `onBlur` event.
* - An `onChange` error will only be cleared on the next `onChange` event.
Comment on lines +165 to +172
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably add an extra line on top like "Some examples:" since those aren't hardcoded rules.

*
* @default false
*/
automaticallyResetValidators?: boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The action this flag enables is that errors are removed when a new validator is run, I think we should better explain this.

I like resetOnRevalidate to indicate that an action (reset) is performed when something happens (onRevalidate) or resetErrorsOnRevalidate if we want to be extra explicit.

What do you think?

}

/**
Expand Down