How to set submit as disabled until form completed?
I have a basic form. 2 fields.
Right now I'm doing the following to set the submit as disabled on page load.
Problem is. When I complete the first field. The submit will be enabled.
The UX that I'm looking for. Is that I only want the submit button to be enabled only when both fields have passed validation.
Is this possible? Thanks!

16 Replies
constant-blue•6mo ago
well, you'll need validators on both field to make sure that they're not empty, presumably onMount and onChange...
Then the forms state will be canSubmit === false until they're filled in
vicious-goldOP•6mo ago
I have onChange validators for both fields and when I console.log canSubmit, it's true on page init
constant-blue•6mo ago
yes, because the validators only fire when the field has changed... you need a validator for onMount to set the fields state as invalid when it is mounted
vicious-goldOP•6mo ago
But won't that show the error messages for the validators?
constant-blue•6mo ago
I think this discussion has a stackBlitz for the same thing you're trying to do. https://github.com/TanStack/form/discussions/1255
GitHub
canSubmit
and isValid
is true
by default? · TanStack form · D...😕 When I build a simple form with validation and subscribe using useStore the initial values for both canSubmit and isValid is true. That seems like an odd choice for initial values, but maybe I...
vicious-goldOP•6mo ago
Thank you! That has exactly what I needed. I wonder why this isn't the default? 🤔
constant-blue•6mo ago
vicious-goldOP•6mo ago
Great, I'll take a look later. Also thanks for your works on the library! Definitely appreciate your contributions on the close to v.1.0!
constant-blue•6mo ago
no worries, always happy to be of help
ambitious-aqua•6mo ago
I would also always be careful with this UX pattern (disabling submit buttons, or disabling anything really).
In this case its only two fields, but on a larger form its easy to miss some required field and then wonder "why is this disabled?". I often find it better to leave the button as is and move the focus to the first invalid field if the user tries to submit with validation errors.
constant-blue•6mo ago
Thats a valid point
vicious-goldOP•6mo ago
I have much more complex forms where this behaviour is desired. It's never been an issue in the past. But thanks anyway.
I followed up on the github issue. Sadly it's not working.
constant-blue•6mo ago
vicious-goldOP•6mo ago
Would it be possible to be able to set the form state by a useEffect? Could that be an easy fix?
I tried doing that but it didn't stick... perhaps there were other mechanisms behind the scenes from preventing this. But something the team could think about?
constant-blue•6mo ago
This was a little more complicated than I though, but know I'm looking in to it... I'll get back to you when i have an answer but I think I know the cause
vicious-goldOP•6mo ago
Oh nice! Thanks for taking a look. Take your time. For me, it's enough that you are looking into it. 😄