Dealing with numerous validations upon submission in Vue.js

Greetings! I have a requirement to check and validate multiple inputs before allowing the user to submit:

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

I have created a method validateErrors to handle individual validations:

   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;
     }
   },

Additionally, I have set up alerts to display respective messages if any of the inputs fail the validation:

<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>

However, I am facing an issue where only the first validation is functioning. How can I handle multiple validations with their own alerts triggered by the same function at submit?

Thank you for your assistance!

Answer №1

One alternative to creating the validateErrors function is to add input rules and utilize form validation in vuejs. Check out this resource for more information: https://v2.vuejs.org/v2/cookbook/form-validation.html. However, if you prefer to continue using the function, you can follow these steps:

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};
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Spin a Collada model on its y-axis

I am struggling with a three.js scene that features a 3D Homer Simpson standing on a plane. My goal is to rotate him around his y-axis using the mouse. The code I have put together is almost there, with pieces borrowed from various sources. However, I am ...

javascript issue with onchange query

The JavaScript snippet below is included in the head section of my file. <?php echo "<script language='JavaScript'>\n"; echo "var times = new Array();\n"; echo "times[0] = 0;\n"; foreach($times as $time) { echo "times[". ...

Update the content within a div based on the selected option from a dropdown menu or

Is there a way to change the displayed text based on user input or selected option? By default, the text shown is "Aa Bb Cc Dd Ee...", but it can be changed by selecting different options. If text is typed into the input field, the displayed text will up ...

Tips for increasing the number of pixels in the current position of an element?

I need to shift an image to the right by adding pixels to its current left position. The challenge arises when the image, positioned absolutely, goes back to its nearest relative parent (the container) and creates a strange visual effect. Within my flex c ...

Assistance needed with selecting elements using jQuery

After some practice with jQuery, I managed to select this specific portion from a lengthy HTML file. My goal is to extract the values of subject, body, and date_or_offset (these are name attributes). How can I achieve this? Let's assume this snippet i ...

Initiating a conversation using a greeting in the Javascript bot framework

I am looking to initiate a multi-level, multiple choice dialog from the welcome message in the Bot Framework SDK using JavaScript. I have a main dialog (finalAnswerDialog) that utilizes LUIS for predicting intents, and a multi-level, multiple choice menu d ...

Launching the webpage with Next.js version 13

Hey there! I'm currently working on implementing a loading screen for my website to enhance the user experience. Since it's quite a large site, I believe a loading screen would be beneficial. However, I'm having trouble getting it to work on ...

Is there a way for me to automatically choose the second checkbox after selecting the first one?

<input type="checkbox" id="first" class="vh-item" value="first" v-model="checkedNames" /> <label class="list-specific" for="first">1stcheck</label> <input type="checkbox" id="second" class="vh-item" v-model="checkedNames" value="secon ...

Why is the code able to execute following the setState hook without any listener declared in the useEffect hook?

Recently, I came across an expo barcode scanner example where a function is executed after a setState hook. This puzzled me as I thought that calling the setState hook would trigger a re-render of the component. Can anyone explain why the function runs aft ...

What is the process for determining the date that is 28 days past a specified date?

I need assistance with finding the 28th day from a date formatted as YYYY-MM-DD. I've attempted various methods without success. Ideally, I would prefer a solution that does not involve Moment.js. Check out my code on Jsfiddle If there are no altern ...

Learn how to create a dynamic React.useState function that allows for an unlimited number of input types sourced from a content management system

Currently, I am exploring the possibility of dynamically creating checkbox fields in a form using DatoCMS and the repeater module. My idea is to generate multiple checkbox fields based on the input from a text field in Dato as a repeater, which can then be ...

Trouble with closing windows in the in-app browser on Android devices

Is there a method to successfully close the in-app browser? Despite window.close working on iOS devices, it is not effective on Android. I have experimented with alternatives like window.top.close and window.open("", "_self") window.close, but none have ...

Utilizing the forEach method for decision-making

Here is an array called 'array' with values [10, 15, 20] var array = [10, 15, 20]; There is also a variable named N with a value of 20: var N = 20; I need to iterate through the array and check if N is greater than all numbers in the array in ...

Endless repetition occurs when invoking a function within a v-for loop

I've encountered an issue while trying to populate an array using a method, leading to redundant data and the following warning message: You may have an infinite update loop in a component render function. Below is the code snippet in question: ...

Tips on successfully executing the swap method in the JustSwap smart contract

I'm attempting to finalize the JustSwap-contract S-USDT-TRX Token (TQn9Y2khEsLJW1ChVWFMSMeRDow5KcbLSE), but I keep receiving a "REVERT opcode executed" response in the console. My code: const TronWeb = require("tronweb"); const ethers ...

Node - ensure that each API call finishes before initiating the next one

Currently, I am encountering an issue with my POST request within the 'add-users' route. I have initialized an array named success and aim to trigger the next API call once a response is received. It seems that the problem lies in sending too ma ...

Determine the variance between the final two items in an array composed of objects

I am trying to figure out how to calculate the difference in total income between the last two months based on their IDs. It seems that {income[1]?.total} always gives me a constant value of 900. Any suggestions on how I can accurately calculate the tota ...

I am looking for a way to retrieve the ids of all div elements that have the same x coordinate using document.elementFromPoint in JavaScript. Can someone help me with

Currently, I am facing an issue where I have two divs positioned at the same x coordinate. I am attempting to retrieve the IDs of both divs using document.elementFromPoint(). However, I am only able to receive the ID of one div. var elem = document.elem ...

How does a JSONP request differ from an AJAX request?

I have searched extensively for a solution to the matter mentioned, yet I did not come across anything intriguing. Would you be able to clarify it in simple terms? ...

Make sure to blur an editable p element using only vanilla JavaScript

Check out this code snippet: <p contenteditable>The most amazing p tag in the world of p tags</p> Here is the corresponding Javascript: var p = document.querySelector('p'); p.addEventListner('keypress', function (e) { ...