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

175
Visualizações
Prevent submitting a form if at least one file is not selected

I am using Vue.js and Axios.

I have two inputs on my html page for selecting an image. There is also a form. The two inputs and the form are separate and independent.

When I select an image, the image is automatically uploaded to the server.

The challenge is how to make it impossible to submit the form until at least one image is selected?

Tell me how to do this?

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Add Item</title>
        <script src="https://unpkg.com/vue@next"></script>
        <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    </head>
    <body>

    <div id="app">

        <form name="addItem" method="post" action="/user/addItem">
            <label for="name">name</label>
            <input type="text" id="name">
            <button type="submit" id="submit">Save Item</button>
        </form>



        <form @submit.prevent enctype="multipart/form-data">
            <input type="file" ref="file1" @change="selectFile1">
        </form>

        <form @submit.prevent enctype="multipart/form-data">
            <input type="file" ref="file2" @change="selectFile2">
        </form>



    </div>


    <script>

        const addProduct = {

            name: "addProduct",

            data() {
                return{
                    file1: "",
                    image1:"",

                    file2: "",
                    image2: "",
                }
            },

            methods:{

                async selectFile1(){
                    const formData = new FormData();
                    this.file1 = this.$refs.file1.files[0];
                    formData.append('file', this.file1);
                    await axios.post('/user/uploadImage', formData)
                },

                async selectFile2(){
                    const formData = new FormData();
                    this.file2 = this.$refs.file2.files[0];
                    formData.append('file', this.file2);
                    await axios.post('/user/uploadImage', formData)
                }

            }
        }

        Vue.createApp(addProduct).mount('#app')
    </script>


    </body>
    </html>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

<form name="addItem" method="post" action="/user/addItem">
    <label for="name">name</label>
    <input type="text" id="name">
    <button type="submit" @click="submitForm" id="submit">Save Item</button>
</form>

<script>
    ...
    submitForm(e) {
        if (!this.file1 && !this.file2) e.preventDefault()
    }
    ...
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

By default your form submit button is disabled until any one of the file has been selected

Step 1: HTML template like

 <template>
  <div id="app">
    <form name="addItem" @submit.prevent="submit">
      <div class="row">
        <label for="name">Name</label>&nbsp;
        <input type="text" id="name" />
      </div>
      <div class="row">
        <input type="file" ref="file1" @change="selectFile1" />
      </div>
      <div class="row">
        <input type="file" ref="file2" @change="selectFile2" />
      </div>
      <div class="row">
        <button type="submit" id="submit" :disabled="!this.file1 && !this.file2">Save Item</button>&nbsp;
        <button type="button" @click.prevent="reset">Reset</button>
      </div>
    </form>
  </div>
</template>

Step 2: methods like

 methods: {
  submit() {
    if (!this.file1 && !this.file2) {
      return false;
    } else {
      const savedata = {
        name: this.name,
      };
      axios.post("/user/addItem", savedata);
    }
  },
  async selectFile1() {
    const formData = new FormData();
    this.file1 = this.$refs.file1.files[0];
    formData.append("file", this.file1);
    await axios.post("/user/uploadImage", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    });
  },
  async selectFile2() {
    const formData = new FormData();
    this.file2 = this.$refs.file2.files[0];
    formData.append("file", this.file2);
    await axios.post("/user/uploadImage", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    });
  },
  reset() {
    this.name = "";
    this.$refs.file1.value = null;
    this.$refs.file2.value = null;
    this.file1 = "";
    this.file2 = "";
  },
},

DEMO Link

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