Skip to content

Commit 945f846

Browse files
committed
fix(form-core): set isValidating on every async validation run
1 parent 2216fde commit 945f846

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/form-core': patch
3+
---
4+
5+
Set `isValidating` on every async field validation run, not only the first one.

‎packages/form-core/src/FieldApi.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,7 @@ export class FieldApi<
15131513

15141514
field.timeoutIds.validations[validateObj.cause] = setTimeout(
15151515
async () => {
1516+
field.timeoutIds.validations[validateObj.cause] = null
15161517
if (controller.signal.aborted) return rawResolve(undefined)
15171518
try {
15181519
rawResolve(

‎packages/form-core/tests/FieldApi.spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,46 @@ describe('field api', () => {
892892
storeunsub()
893893
})
894894

895+
it('should set isValidating on every async validation run', async () => {
896+
// Test for https://github.com/TanStack/form/issues/2372
897+
vi.useFakeTimers()
898+
899+
const form = new FormApi({
900+
defaultValues: {
901+
name: '',
902+
},
903+
})
904+
905+
form.mount()
906+
907+
const field = new FieldApi({
908+
form,
909+
name: 'name',
910+
validators: {
911+
onChangeAsync: async ({ value }) => {
912+
await sleep(1000)
913+
return value === 'admin' ? 'Username is already taken' : undefined
914+
},
915+
},
916+
})
917+
918+
field.mount()
919+
920+
field.setValue('admin')
921+
await vi.advanceTimersByTimeAsync(0)
922+
expect(field.getMeta().isValidating).toBe(true)
923+
await vi.runAllTimersAsync()
924+
expect(field.getMeta().isValidating).toBe(false)
925+
expect(field.getMeta().errors).toStrictEqual(['Username is already taken'])
926+
927+
field.setValue('asdf')
928+
await vi.advanceTimersByTimeAsync(0)
929+
expect(field.getMeta().isValidating).toBe(true)
930+
await vi.runAllTimersAsync()
931+
expect(field.getMeta().isValidating).toBe(false)
932+
expect(field.getMeta().errors).toStrictEqual([])
933+
})
934+
895935
it('should run async validation onChange', async () => {
896936
vi.useFakeTimers()
897937

0 commit comments

Comments
 (0)