Validation onSubmit does not re-trigger with conditionally rendered fields

Aperta
#1,874 3 commenti 3 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
react, typescript
Ambito
frontend, testing

Direzione di ricerca

Inizia con la riproduzione minima collegata di StackBlitz e segui la sequenza di invio: invia una volta, modifica il campo condizionale, quindi invia di nuovo. Traccia il percorso di validazione del form React e confronta il comportamento di onSubmit con quello di onChange; il lavoro è completo quando il secondo invio valida in modo coerente il campo appena renderizzato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

v1 v2: needs investigation
Describe the bug

The onSubmit validator is not re-triggering after first time when a "dynamic" field has changed. In this case I'm using Zod's discriminated union to decide whether to show this or that field, but this bug happens also when using for example a superRefine to conditionally validate a field. The first submit triggers correctly the onSubmit validator, but then if we change the field and show the other input field and submit again, it does not work trigger the validator. Even with canSubmitWhenInvalid: true.

There is a similar issue #1780 that points to this error but it's not actually the same. In that issue the author resolved the issue just putting canSubmitWhenInvalid: true, but actually the problem we have found is in the validation not getting triggered.

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-q2pndiqf?file=src%2Findex.tsx

Steps to reproduce
  1. Click on Submit -> Should show the error for thisField
  2. Change the Select to the value That
  3. Click again on Submit -> Should show the error for thatField, but it does not.

If we change the validators to run on onChange, it works!
It works also if after submitting you change an input or on blur.

Expected behavior

I would expect that the onSubmit validator would run on submitting the form and it would have the same result of the onChange schema validation

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform
  • OS: MacOS
  • Browser: Chrome
  • Version: 142.0.7444.60
TanStack Form adapter

react-form

TanStack Form version

1.25.0

TypeScript version

5.9.3

Additional context

No response

Lingua principale
TypeScript
Stelle
6.8k
Fork
717
Merge medio
5g 22h
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/form

Tutte le issue di TanStack/form

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.