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

fix(core): run all types of async validation on fields during submit #852

Merged
Merged
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
40 changes: 40 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,46 @@ describe('form api', () => {
])
})

it('should run all types of async validation on fields during submit', async () => {
vi.useFakeTimers()

const form = new FormApi({
defaultValues: {
firstName: '',
lastName: '',
},
})

const field = new FieldApi({
form,
name: 'firstName',
validators: {
onChangeAsync: async ({ value }) => {
await sleep(1000)
return value.length > 0 ? undefined : 'first name is required'
},
onBlurAsync: async ({ value }) => {
await sleep(1000)
return value.length > 3
? undefined
: 'first name must be longer than 3 characters'
},
},
})

field.mount()

form.handleSubmit()
expect(form.state.isFieldsValid).toEqual(true)
await vi.runAllTimersAsync()
expect(form.state.isFieldsValid).toEqual(false)
expect(form.state.canSubmit).toEqual(false)
expect(form.state.fieldMeta['firstName'].errorMap).toEqual({
onChange: 'first name is required',
onBlur: 'first name must be longer than 3 characters',
})
})

it('should clear onSubmit error when a valid value is entered', async () => {
const form = new FormApi({
defaultValues: {
Expand Down