在sweetalert2中显示来自preConfirm的错误消息

问题描述 投票:0回答:2

Im使用sweetalert2sweetalert2-react-content。我的inputValidator工作正常,向用户显示了错误。如何在有AJAX请求的preConfirm中执行相同的操作?

    MySwal.fire({
        title: 'title',
        input: 'text',
        html: this.renderInfoText(),
        showLoaderOnConfirm: true,
        inputValidator: (value) => {
            return new Promise((resolve) => {
              if (this.validate(value)) { 
                resolve()
              } else {
                resolve('check formatting...')
              }
            })
        },            
        preConfirm: (pno) => {
            return axios.post('/some/route', { pno })
                .then(response => { 
                    // ...
                })
                .catch(error => {
                    // show an error message to the user from here?
                });
        }
    }) 
reactjs sweetalert2
2个回答
0
投票

这实际上记录在examples下的AJAX request example中。这是我的解决方案:

MySwal.fire({
    title: 'title',
    input: 'text',
    html: this.renderInfoText(),
    showLoaderOnConfirm: true,
    inputValidator: (value) => {
        return new Promise((resolve) => {
          if (this.validate(value)) { 
            resolve()
          } else {
            resolve('check formatting...')
          }
        })
    },            
    preConfirm: (pno) => {
        return axios.post('/some/route', { pno })
            .then(response => { 
                // ...
            })
            .catch(error => {
                if (error.response.status === 404) {
                    MySwal.showValidationMessage(error.response.data.message)                        
                }                        
            });
    }
}) 

-1
投票

here在某些示例中,他们正在使用访存,请尝试使用它

MySwal.fire({
        title: 'title',
        input: 'text',
        html: this.renderInfoText(),
        showLoaderOnConfirm: true,
        inputValidator: (value) => {
            return new Promise((resolve) => {
              if (this.validate(value)) { 
                resolve()
              } else {
                resolve('check formatting...')
              }
            })
        },            
        preConfirm: (pno) => {
            return fetch('/some/route', {
                method: 'POST',
                body: JSON.stringify(pno),
                headers: {
                'Content-Type': 'application/json'
            })
                .then(response => { 
                    // ...
                })
                .catch(error => {
                    // show an error message to the user from here?
                });
        }
    }) 
© www.soinside.com 2019 - 2024. All rights reserved.