I'm trying to validate the email
field before closing the SweetAlert
but for some reason the SweetAlert
is closing after catching the error:
Swal.fire({
title: title,
html: template,
preConfirm: function (email) {
email = '';
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (email === '') {
alert("err");
reject('err')
} else {
resolve()
}
}, 1000)
}).catch(err => alert("error catched"))
},
});
What can I do to prevent the SweetAlert
modal from closing when the error is caught?
Thanks
I'm trying to validate the email
field before closing the SweetAlert
but for some reason the SweetAlert
is closing after catching the error:
Swal.fire({
title: title,
html: template,
preConfirm: function (email) {
email = '';
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (email === '') {
alert("err");
reject('err')
} else {
resolve()
}
}, 1000)
}).catch(err => alert("error catched"))
},
});
What can I do to prevent the SweetAlert
modal from closing when the error is caught?
Thanks
Share Improve this question edited Feb 19, 2020 at 17:10 Souleste 1,9841 gold badge14 silver badges42 bronze badges asked Sep 8, 2019 at 14:01 sfarzososfarzoso 1,6407 gold badges31 silver badges83 bronze badges 5-
Maybe try to remove the
alert("err");
which is before thereject
as it would do a double alert? – pistou Commented Sep 8, 2019 at 15:15 - @pistou same problem, sweetalert is closing and the error still appear – sfarzoso Commented Sep 8, 2019 at 15:20
-
Seems weird to me that you're about to return a Promise, but instead, you return it with a
.catch
appended to it. Try just returning the promise? – Andrew Koster Commented Sep 8, 2019 at 16:53 -
@AndrewKoster I return the promise, perhaps do you mean something else? my code is:
return new Promise(function (resolve, reject) {
– sfarzoso Commented Sep 8, 2019 at 17:41 -
1
I re-read the documentation for
Promise
, and saw that there's nothing wrong with that part of your code. See my answer for the actual solution. – Andrew Koster Commented Sep 8, 2019 at 18:13
1 Answer
Reset to default 4Here's an example that's fully functional (for solving your specific problem):
<html>
<body>
<div class="showDialogButton">Show dialog</div>
<script src="https://code.jquery./jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr/npm/sweetalert2@8"></script>
<script type="text/javascript">
$(() => {
const title = "Hi"
const template = `<b>hello</b> world`
$(".showDialogButton").click(() => {
Swal.fire({
title: title,
html: template,
preConfirm: function (email) {
email = '';
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (email === '') {
reject('Email is empty!')
} else {
resolve()
}
}, 1000)
}).catch(err => {
alert(`error: ${err}`)
return false
})
},
});
})
})
</script>
</body>
</html>
The default behavior is to close the dialog. In order to override that, you have to return false
from the function that you're passing in to .catch
.
From https://sweetalert2.github.io/#configuration
Argument: preConfirm
Default value: null
Description: Function to execute before confirm, may be async (Promise-returning) or sync. Returned (or resolved) value can be:
false
to prevent a popup from closing- anything else to pass that value as the
result.value
ofSwal.fire()
undefined
to keep the defaultresult.value
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272550a4619836.html
评论列表(0条)