javascript - Cannot prevent sweet alert from closing - Stack Overflow

I'm trying to validate the email field before closing the SweetAlert but for some reason the Sweet

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 the reject 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
Add a ment  | 

1 Answer 1

Reset to default 4

Here'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 of Swal.fire()
  • undefined to keep the default result.value

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272550a4619836.html

相关推荐

  • javascript - Cannot prevent sweet alert from closing - Stack Overflow

    I'm trying to validate the email field before closing the SweetAlert but for some reason the Sweet

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信