Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

562
Visualizações
Confirm values are set Sweet Alert (multiple input) with Async

I am trying to add validation to a multiple-input modal I set up with SweetAlert2.

I am using the below code, modified from https://sweetalert2.github.io/#multiple-inputs

 jQuery('body').on('click', '#feedback', async function () {
  const {value: feedback} = await swal.fire ({
  title: 'Send us your feedback!',
  showCancelButton: true,
  confirmButtonText: 'Submit',
  denyButtonText: 'Cancel',
    customClass: {
    content:'full-width'
    },
    html:
    '<input id="ratingValue" type="text" />'+
    '<input id="topicValue" type="text" />'+
    '<input id="comment" type="text" />'
     ,
    preConfirm: () => ({
      rating: jQuery('#ratingValue').val(),
      topic: jQuery('#topicValue').val(),
      comment: jQuery('#comment').val()
      
    })
  }).then((result) => {
      console.log(JSON.stringify(feedback));
      if(feedback.rating != "" && feedback.topic != ""){
         Swal.fire('Submitted!', '', 'success')
      }
  });

Obviously, I have a problem, I am passing the feedback object as a pass by promise parameter, so I can't try to call it from the (then) state as the variable hasn't been initialised yet and I get an error. Is there any workaround for this? I would like to validate my inputs before submitting, and be able to allow the user to correct himself.

Thanks a lot!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

.on('click' takes a callback function, it doesn't return a Promise, so you can't simply chain .then() to it. You can do something like this, I guess :

jQuery('body').on('click', '#feedback', clickHandler);

const clickHandler = async () => {

  const feedback = await swal.fire({...});

  console.log(JSON.stringify(feedback));
  if (feedback.rating != "" && feedback.topic != "") {
    Swal.fire('Submitted!', '', 'success')
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda