Capítulo 19 de 29
The library intentionally has no built-in focus-on-error behavior (it doesn't know your markup), so focusing the first invalid field on a failed submit is a few lines you write yourself in onSubmitInvalid.
onSubmitInvalid fires when handleSubmit() runs but validation fails — the natural hook point for focus management.[aria-invalid="true"] and call .focus() on the first match — TanStack Form's fields set aria-invalid as part of their accessibility state, so this selector is reliable without extra wiring.document.querySelector, track input refs yourself and iterate the form's error map to find and focus the first problematic field's ref.onSubmitInvalid() {
const invalidInput = document.querySelector('[aria-invalid="true"]') as HTMLInputElement
invalidInput?.focus()
}
onSubmitInvalid focus logic will not move focus anywhere on a failed submit.aria-invalid rather than a custom "has error" class — it's already set by the library and doubles as the accessibility signal screen readers use.onSubmitInvalid as the counterpart to a successful onSubmit.