Uniforms: [QUESTION]: Cancel form submission.

Created on 30 Dec 2020  路  2Comments  路  Source: vazco/uniforms

Hi,

I have a quick question.

In a form, using GraphQLBridge, I am validating some fields before triggering the onSubmit.

In a specific case, I want to validate if a field is selected, and if it is, then show a confirmation dialog, and if the user confirms, then proceed with the onSubmit.

My question is, if the user cancels, how do I prevent the form from proceeding to the onSubmit from within the validator function?

Is there an easy way to achieve something like this?

Hope I am clear, thanks!

question

All 2 comments

Hi @simplecommerce. No, there's no way to cancel submit on the uniforms side, but nothing blocks you from doing that in your code (e.g. save the request made in onSubmit and cancel it after certain action). However, your use case suggests, that there's no need to do that - don't submit the form but validate it instead.

It would go like this: keep the form ref and create a button that will initiate your saving process, i.e.:

async function start() {
  const error = await formRef.validate();
  if (error !== null) return;
  const confirmed = await askUserUsingDialog();
  if (!confirmed) return;
  await formRef.submit();
}

@radekmie Ok that is what I had assumed, thanks for your help and example, it will help me with my use case!

Happy new years!

Was this page helpful?
0 / 5 - 0 ratings