Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/wild-moons-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/form-core': patch
---

Report `isValidating` on every async validation run instead of only the first one.
8 changes: 8 additions & 0 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,14 @@ export class FieldApi<

field.timeoutIds.validations[validateObj.cause] = setTimeout(
async () => {
// The timer has fired, so this run is no longer waiting on a
// debounce. Release the id right away: the branch above uses a
// stored id to mean "a previous run is still pending" and
// compensates for it with `endValidation()`. Leaving a fired id
// behind makes the next run decrement the pending-validation
// counter for a run that already finished, which clears
// `isValidating` while that next run is still in flight.
field.timeoutIds.validations[validateObj.cause] = null
if (controller.signal.aborted) return rawResolve(undefined)
try {
rawResolve(
Expand Down
44 changes: 44 additions & 0 deletions packages/form-core/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,50 @@ describe('field api', () => {
storeunsub()
})

it('should set isValidating again on every async validation run after the first', async () => {
// Test for https://github.com/TanStack/form/issues/2372
vi.useFakeTimers()

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

form.mount()

const field = new FieldApi({
form,
name: 'name',
validators: {
onChangeAsync: async ({ value }) => {
await sleep(1000)
if (value === 'admin') return 'Username is already taken'
return
},
},
})

field.mount()

// First run: isValidating is reported while the validator is pending.
field.setValue('admin')
await vi.advanceTimersByTimeAsync(0)
expect(field.getMeta().isValidating).toBe(true)
await vi.runAllTimersAsync()
expect(field.getMeta().isValidating).toBe(false)
expect(field.getMeta().errors).toContain('Username is already taken')

// Second run: the validator is pending again, so isValidating must be
// reported again instead of staying false for the whole run.
field.setValue('asdf')
await vi.advanceTimersByTimeAsync(0)
expect(field.getMeta().isValidating).toBe(true)
await vi.runAllTimersAsync()
expect(field.getMeta().isValidating).toBe(false)
expect(field.getMeta().errors.length).toBe(0)
})

it('should run async validation onChange', async () => {
vi.useFakeTimers()

Expand Down