Differential validation in onSubmit depending on onSubmitMeta
Hi! I'm using the onSubmitMeta to include different submitActions for onSubmit: "autosave", "publish" and "unpublish". This gives me the ability to use the isSubmitting and keep it within the form, but I would like to have different validation schemas for the different submitActions, as the user should be able to save their draft without it necessarily being complete, but not be able to publish until all the pieces are in place. I have tried moving the validation to within the onSubmit function and the switch cases, but I'm unsure of how to transfer the validation response to the form so that I can access validation errors from the fields (like it automatically does with the validators property). Is there any native way for me to go about this or do I have to get creative?

9 Replies
old-apricot•5mo ago
if you need to forward the validation errors to the fields, then the function's purpose is to be a validator. This logic should be moved into
validators.onSubmitAsync. If onSubmitAsync passes successfully, it will be considered a successful submission and trigger whatever's left in onSubmit (or omit it entirely)sunny-greenOP•5mo ago
But with validators.onSubmitAsync, since every submission behavior is in onSubmit all my submitActions will still have the same validation requirements, right? I would like for the autosave submitAction to have a more forgiving schema since it's essentially saving a draft while publish needs the completed form. Can I pass the submitMeta into the validator in some way?
old-apricot•5mo ago
hmm ... currently, you don't have access to meta in
onSubmitAsync, right?sunny-greenOP•5mo ago
It doesn't seem like it, no.
old-apricot•5mo ago
that's unfortunate. We've planned on adding a
createError function to onSubmit, but regardless of that planned feature, I feel like validators.onSubmit and validators.onSubmitAsync should have access to the meta. Could you create a feature request for it on GitHub? If not, I can create one latersunny-greenOP•5mo ago
I'll create one, thank you for the assistance!
ratty-blush•4mo ago
When you say feature req, would that be an idea or an issue with feature title/enhancement tag? I see that we currently have an idea for it, but wonder if an issue would be better for tracking?
old-apricot•4mo ago
the idea section for discussions is best for this. Issues should ideally only be for issues that need resolving
that being said, there's lots of feature requests on the issues section. The tag is meant to help with determining what's what, but it's more work than just using the intended path
ratty-blush•4mo ago
yea that's what made me unsure, but all is well then, thanks!