T
TanStack4d ago
optimistic-gold

Always trigger onSubmit even when the form is invalid?

Hey there, Is there a way to always trigger onSubmit even when the form is invalid. I kind of expected setting the canSubmitWhenInvalid argument would have this effect, but it only has an effect on the canSubmit flag. I could just use the same function for onSubmitInvalid, but then I do have to manage some of the form state myself which is not ideal I would prefer to use the submission state variables. The use case is a highly dynamic configuration based form and we don't differentiate between form actions all of them trigger a submit with an Id in the metadata. So in the case of save as draft when the user does not care if the form is valid or not we also want to trigger a submit which seems to be not possible currently. Thanks in advance
5 Replies
adverse-sapphire
adverse-sapphire4d ago
what's the end goal here? You have validation that prevents onSubmit, yet you want onSubmit to always be ran. if it should always run regardless of form state, you can do the action outside of form.handleSubmit
optimistic-gold
optimistic-goldOP4d ago
Because validation is always done server side we always want to execute the submit regardless of the validation state in the frontend. We still use the validation state for display purpose towards the user. The reason I would like to do it via the submit and not outside is because I want it integrated in the form state.
adverse-sapphire
adverse-sapphire4d ago
There's only a few things in the form state that are affected by the submit process: * validation * isSubmitSuccessful * isSubmitted * submissionAttempts The main request here is that the submission process is disconnected from validation, which is currently not possible. One way this could be solved is that submit meta is available in validators (and then not validate when it's a draft), but this is not the case yet. Triggering onSubmit everytime is a request that's likely not going to be part of the API anytime soon. If you don't rely on the state mentioned above, then having the function outside of form.handleSubmit is not a problem.
optimistic-gold
optimistic-goldOP4d ago
Ok that's clear, thanks for the explanation. It's a bit of a niche case I know. I'm currently using some of the submission state so I'll see if I can override them or handle it completely outside. Thanks again!
adverse-sapphire
adverse-sapphire4d ago
let me know if you need some help with the implementation details

Did you find this page helpful?