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

168
Visualizações
How to handle multiple validations on submit (Vue.js)

Hi there, I need to validate several inputs before letting the user submit:

  <button type="submit" v-on:click="validateErrors()" @click.prevent="submit">Finalizar</button>

I did a method validateErrors in order to handle all of the validations separately:

   validateErrors() {
     if (this.campaign.selectedBox == null) {
      this.$set(this.msg, 'selectedbox', 'Alert 1') ;
      return true;
     }
     if (this.campaign.selectedExtras == null) {
      this.$set(this.msg, 'selectedextras', 'Alert 2') ;
      return true;
     }
   },

Lastly, I made an alert with the different messages to show in case one (or various) of the inputs don't validate:

<div class="alert alert-info w-100" v-if="msg.selectedbox">{{msg.selectedbox}}</div>
<div class="alert alert-info w-100" v-if="msg.selectedextras">{{msg.selectedextras}}</div>

Only the first of the validations is working, how can I handle multiple validations with their own alerts being executed by the same function on submit?

Thanks a lot!

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

0

Instead of creating this function you can add rules for the input and then use form validator in vuejs https://v2.vuejs.org/v2/cookbook/form-validation.html But if you want to stay using this function you can do this

function validateErrors() {
  var check1 = false;
  var check2 = false;
  if (this.campaign.selectedBox == null) {
    this.$set(this.msg, "selectedbox", "Alert 1");
    check1 = true;
  }
  if (this.campaign.selectedExtras == null) {
    this.$set(this.msg, "selectedextras", "Alert 2");
    check2 = true;
  }
  return {check1:check1, check2:check2};
}
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