Replies: 2 comments
-
|
This isn't really an async validation problem, it's a focus race. When you press down on the submit button, the input loses focus first, The fix is to stop the input from blurring on the press. Put <button
type="submit"
onMouseDown={(e) => e.preventDefault()}
>
Submit
</button>
One thing to watch: because the input no longer blurs on that press, const form = useForm({
validators: {
onSubmitAsync: async ({ value }) => {
// same async check you had in onBlurAsync
},
},
})
|
Beta Was this translation helpful? Give feedback.
-
|
This is a browser behavior rather than a TanStack Form quirk, and it shows up specifically because of how the submit button's disabled state is likely wired. Pressing down on the submit button while the field still has focus fires that field's blur before the click completes, which is what kicks off TanStack Form's own docs already call out the fix for this, for a different reason (accessibility), which happens to solve this too: "disabled buttons are not accessible, use <button
type="submit"
aria-disabled={!canSubmit}
onClick={(e) => {
if (!canSubmit) e.preventDefault()
}}
>
Submit
</button>Since |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
If i use onBlurAsync on a field, when I edit the field with a valid value if I click the submit button while the cursor still being on the field then the onBlurAsync will be called and the submit will not be registered :
example stackblitz
Do you have any guidance on how to have async blur validation + submit with 1 click in this scenario ?
Also in my app form using createFormHook, useFormContext, withForm, ... I have 2 onBlurAsync called, the submit button flickering and sometimes a submit sometimes not.
Beta Was this translation helpful? Give feedback.
All reactions