question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow preConfirm to prevent dialog closing

See original GitHub issue

It would be nice to be able to keep the dialog open if some condition is not met in preConfirm logic.

Previously it was possible by returning a rejected promise, but with v. 7 it looks like the only way to prevent closing is to call swal.showValidationError.

Usage example, if I want user to confirm a destructive action:

preConfirm: function() {
  if (!confirm('Are you sure?')) {
    // possible solution - a flag that would instruct swal to keep the dialog open
    swal.keepOpened = true;
    return;
  }
  // proceed otherwise
}

As a workaround I had to do this:

onOpen: function(el) {
  var container = $(el);
  var originalConfirmButton = container.find('.swal2-confirm');
  var clonedConfirmButton = originalConfirmButton.clone();

  originalConfirmButton.hide();
  clonedConfirmButton.insertAfter(originalConfirmButton);

  clonedConfirmButton.on('click', function() {
    if (confirm('Are you sure?')) originalConfirmButton.click();
  });
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gvernicommented, May 23, 2019

@BMahabir can you provide a live example? BTW, if you want to see preConfirn in action you can have a look to this example (source code here)

1reaction
zenflowcommented, Feb 8, 2018

I didn’t suggest that initially because it would only work for sync. code.

Not sure I understand this statement. Sync and async preConfirms should work the same as of version 7.

Could it also cause result.value to be falsy in swal().then(), making it look like user clicked “Cancel”?

Nope. I had a similar concern too, but if the result of preConfirm is falsy, then the original value passed to preConfirm is used. If there is no input option, result.value will be true.

https://github.com/sweetalert2/sweetalert2/blob/8cc05c8823a50589cd735cf00f16596556a1c5aa/src/sweetalert2.js#L576-L585

Besides, if preConfirm returns false, then there will be no result at all for that case, since the swal will not complete. My concern was that you would no longer be able to use preConfirm to make the result.value a boolean (possibly false), but this was never possible, for the above reason.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent SweetAlert2 from closing/dismissing from willClose?
No, you can't prevent dialog from closing with willClose, maybe the following code can be your alternative: await Swal.fire({ html: diagHtml ...
Read more >
How to Prevent closing of modal Dialog in JavaScript (ES5 ...
You can prevent closing of modal dialog by setting the beforeClose event argument cancel value to true. In the following sample, the dialog...
Read more >
SweetAlert2 - a beautiful, responsive, customizable and ...
A confirm dialog, with a function attached to the "Confirm"-button ... let timerInterval Swal.fire({ title: 'Auto close alert!', html: 'I will close in ......
Read more >
Dialog Component - VueTailwind
VueJs Dialog component with configurable classes and infinite variants. ... disabled:cursor-not-allowed w-full max-w-xs', okButton: 'block px-4 py-2 ...
Read more >
TSA PreCheck expedited screening for members of CBP ...
Name mismatch may prevent you from receiving benefits. Ensure that the documents on your TTP account are up to date and that your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found