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

Deduplicate field and form errors property #908

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,13 @@ export class FieldApi<
onUpdate: () => {
const state = this.store.state

state.meta.errors = Object.values(state.meta.errorMap).filter(
(val: unknown) => val !== undefined,
)
state.meta.errors = [
...new Set(
Object.values(state.meta.errorMap).filter(
(val: unknown) => val !== undefined,
),
),
]

state.meta.isPristine = !state.meta.isDirty

Expand Down
10 changes: 7 additions & 3 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,13 @@ export class FormApi<
const isPristine = !isDirty

const isValidating = isFieldsValidating || state.isFormValidating
state.errors = Object.values(state.errorMap).filter(
(val: unknown) => val !== undefined,
)
state.errors = [
...new Set(
Object.values(state.errorMap).filter(
(val: unknown) => val !== undefined,
),
),
]
const isFormValid = state.errors.length === 0
const isValid = isFieldsValid && isFormValid
const canSubmit =
Expand Down
1 change: 0 additions & 1 deletion packages/form-core/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,6 @@ describe('field api', () => {
field.validate('blur')
expect(field.getMeta().errors).toStrictEqual([
'Please enter a different value',
'Please enter a different value',
])
expect(field.getMeta().errorMap).toEqual({
onBlur: 'Please enter a different value',
Expand Down
5 changes: 1 addition & 4 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,10 +1160,7 @@ describe('form api', () => {

field.setValue('other')
field.validate('blur')
expect(form.state.errors).toStrictEqual([
'Please enter a different value',
'Please enter a different value',
])
expect(form.state.errors).toStrictEqual(['Please enter a different value'])
expect(form.state.errorMap).toEqual({
onBlur: 'Please enter a different value',
onChange: 'Please enter a different value',
Expand Down