Skip to content

Commit

Permalink
feat: add setErrorMap to formApi and fieldApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Toavina23 authored and Balastrong committed Aug 6, 2024
1 parent 5a13ea9 commit 20c3109
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,19 @@ export class FieldApi<
}
this.validate('blur')
}

/**
* Updates the field's errorMap
*/
setErrorMap(errorMap: ValidationErrorMap) {
this.setMeta((prev) => ({
...prev,
errorMap: {
...prev.errorMap,
...errorMap,
},
}))
}
}

function normalizeError(rawError?: ValidationError) {
Expand Down
12 changes: 12 additions & 0 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,18 @@ export class FormApi<
this.validateField(`${field}[${index1}]` as DeepKeys<TFormData>, 'change')
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
}
/**
* Updates the form's errorMap
*/
setErrorMap(errorMap: ValidationErrorMap) {
this.store.setState((prev) => ({
...prev,
errorMap: {
...prev.errorMap,
...errorMap,
},
}))
}
}

function normalizeError(rawError?: ValidationError) {
Expand Down
59 changes: 59 additions & 0 deletions packages/form-core/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1270,4 +1270,63 @@ describe('field api', () => {
'Passwords do not match',
])
})

it('should add a new value to the fieldApi errorMap', () => {
interface Form {
name: string
}
const form = new FormApi<Form>()
const nameField = new FieldApi({
form,
name: 'name',
})
nameField.mount()
nameField.setErrorMap({
onChange: "name can't be Josh",
})
expect(nameField.getMeta().errorMap.onChange).toEqual("name can't be Josh")
})
it('should preserve other values in the fieldApi errorMap when adding other values', () => {
interface Form {
name: string
}
const form = new FormApi<Form>()
const nameField = new FieldApi({
form,
name: 'name',
})
nameField.mount()
nameField.setErrorMap({
onChange: "name can't be Josh",
})
expect(nameField.getMeta().errorMap.onChange).toEqual("name can't be Josh")
nameField.setErrorMap({
onBlur: 'name must begin with uppercase',
})
expect(nameField.getMeta().errorMap.onChange).toEqual("name can't be Josh")
expect(nameField.getMeta().errorMap.onBlur).toEqual(
'name must begin with uppercase',
)
})
it('should replace errorMap value if it exists in the fieldApi object', () => {
interface Form {
name: string
}
const form = new FormApi<Form>()
const nameField = new FieldApi({
form,
name: 'name',
})
nameField.mount()
nameField.setErrorMap({
onChange: "name can't be Josh",
})
expect(nameField.getMeta().errorMap.onChange).toEqual("name can't be Josh")
nameField.setErrorMap({
onChange: 'other validation error',
})
expect(nameField.getMeta().errorMap.onChange).toEqual(
'other validation error',
)
})
})
39 changes: 39 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1527,4 +1527,43 @@ describe('form api', () => {
{ nameInfo: { first: 'firstName' } },
])
})
it('should add a new value to the formApi errorMap', () => {
interface Form {
name: string
}
const form = new FormApi<Form>()
form.setErrorMap({
onChange: "name can't be Josh",
})
expect(form.state.errorMap.onChange).toEqual("name can't be Josh")
})
it('should preserve other values in the formApi errorMap when adding other values', () => {
interface Form {
name: string
}
const form = new FormApi<Form>()
form.setErrorMap({
onChange: "name can't be Josh",
})
expect(form.state.errorMap.onChange).toEqual("name can't be Josh")
form.setErrorMap({
onBlur: 'name must begin with uppercase',
})
expect(form.state.errorMap.onChange).toEqual("name can't be Josh")
expect(form.state.errorMap.onBlur).toEqual('name must begin with uppercase')
})
it('should replace errorMap value if it exists in the FormApi object', () => {
interface Form {
name: string
}
const form = new FormApi<Form>()
form.setErrorMap({
onChange: "name can't be Josh",
})
expect(form.state.errorMap.onChange).toEqual("name can't be Josh")
form.setErrorMap({
onChange: 'other validation error',
})
expect(form.state.errorMap.onChange).toEqual('other validation error')
})
})

0 comments on commit 20c3109

Please sign in to comment.