Allow preConfirm to prevent dialog closing
See original GitHub issueIt 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:
- Created 6 years ago
- Reactions:11
- Comments:10 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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)Not sure I understand this statement. Sync and async
preConfirm
s should work the same as of version 7.Nope. I had a similar concern too, but if the result of
preConfirm
is falsy, then the original value passed topreConfirm
is used. If there is noinput
option,result.value
will betrue
.https://github.com/sweetalert2/sweetalert2/blob/8cc05c8823a50589cd735cf00f16596556a1c5aa/src/sweetalert2.js#L576-L585
Besides, if
preConfirm
returns false, then there will be noresult
at all for that case, since the swal will not complete. My concern was that you would no longer be able to usepreConfirm
to make theresult.value
a boolean (possiblyfalse
), but this was never possible, for the above reason.