Vue JS - preventing button from being enabled until all validation errors have been resolved

I am facing an issue with my button that is supposed to be disabled until all validation requirements are met. Currently, the button only gets disabled after I click it for the first time and encounter validation errors. I need the button to be disabled right from the start until all inputs are correctly entered. Can anyone provide assistance with this matter?

Form component

<template>
  <div class="form-wrapper">
    <form @submit.prevent="handleSubmit(sendFormData)" novalidate>
      <slot name="form-content"/>
      <slot name="button-submit" :is-invalid="isInvalid"/>
    </form>
  </div>
</template>

inputs component (only showing the button section for this part):

<template #button-submit="{isInvalid}">
   <button :disabled="isInvalid && !selectedTown"
      :class="{'button-main-disabled': isInvalid && !selectedTown}"
      type="submit"
      class="button-main button-add">
      Add School
   </button>
</template>

Answer №1

To start, if isInvalid is set as a data property, make sure to initialize it as true, and only update its value to false once the inputs are confirmed as valid (in the handleSubmit() method):

export default {
  data() {
    return {
      isInvalid: true 👈
    }
  },
  methods: {
    handleSubmit() {
      // validate data...

      this.isInvalid = false /* data is valid */
    }
  }
}

Another thing to note is that your usage of the disabled binding might not be accurate. It currently disables the button when the form is in an invalid state AND no town is selected, but perhaps you intended for an OR condition (assuming that selecting a town is mandatory).

:disabled="isInvalid && !selectedTown" ❌
                     ^^

:disabled="isInvalid || !selectedTown" ✅

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

Transformation of Array of Objects to Array of Values using Node.js

I am looking to transform the following: [{ prop1: '100', prop2: false, prop3: null, prop4: 'abc' }, { prop1: '102', prop2: false, prop3: null, prop4: 'def' } ] into this format: [[100,false,null,'abc&ap ...

What is the best way to toggle the visibility of fields on a modal in a Ruby on Rails

I am working on an application that utilizes Rails 4.1 and Ruby 2.1.2. The show.html.erb view for the Assignment Model in this application is as follows: <h1><%= "#{'Source URL'}"%> <div class="source-url"><%= @assignment.so ...

Performing various tasks using a Single Change Event in JQuery/JavaScript

Looking for assistance with implementing this javascript to automatically populate various fields when a selection is made in a dropdown menu. <select class="form-control" name='brands_list'> <option value="0">Seleziona il produttore ...

Loop through a JSON object using a sequence of setTimeout() functions

After running another function, I have retrieved a JSON object stored in the variable 'json_result'. My objective is to log each individual JSON part (e.g. json_result[i]) after waiting for 5 seconds. Here was my initial attempt: for (let key ...

I attempted to utilize the prop to transfer an object, but unfortunately, the value was not received

I am working with a child component that accepts a todo object. Vue.component('child', { props: ['todo'], template: '<span>{{ todo.text }}</span>' }) var vm = new Vue({ el: '# ...

Expanding a table by including a new column using Node.js and Knex

Currently building a service for my router using Node.js and Knex, but I'm struggling to add a column to an existing table. Any assistance would be greatly appreciated. Even though I am working with PostgreSQL, I believe the solution will be similar r ...

The functionality of the Regions plugin in wavesurfer.js appears to be completely nonfunctional

I've been attempting to utilize the Regions plugin from wavesurfer.js, but unfortunately, it's not functioning as expected. Despite trying various methods suggested on different websites, I have yet to achieve success. Below is the snippet of c ...

Using node.js to set the billing address while confirming a Stripe Payment intent

Is there a way to specify the billing address of a payment intent with Node.js? When using the Stripe.js browser-side framework, I can easily accomplish this by utilizing Stripe elements and the stripe.confirmPayment function, which automatically captures ...

Can anyone guide me on troubleshooting the firebase import error in order to resolve it? The error message I am encountering is "Module not found: Error:

When attempting to import the 'auth' module from my 'firebase.js' file, I encountered an error. I used the code import {auth} from "./firebase", which resulted in the following error message: Error: Unable to locate module &a ...

I am looking to create a cascading dropdown list in C# MVC that pulls data from various SQL tables. How can I achieve

I am currently working on a C# MVC web application and facing a challenge in implementing a cascading drop-down list. While I have come across various articles discussing this topic, the uniqueness of my requirements makes it difficult for me to figure out ...

What could be causing the dropdownlist value not to be selected or checked while using "Select2.js"?

1-Description I have implemented a select2 filter for a dropdown list in my project. (Tools used: Bootstrap 5 & .Net 7.0 Core) When I choose a client name and click submit, an error occurs stating that the 'clientId' is null (indicating no ...

The inefficiencies of the Vue composition API: Reasons why it may be failing to meet your expectations

Currently, I am troubleshooting an issue within the vue editor js package. The problem lies with the "loadData" button, which is successfully changing the prop initData. However, the Editor component does not seem to re-render as expected. Despite my expe ...

Latitude and longitude coordinates in Leaflet Vue.js have been successfully updated

const updateLocation = () => { this.longitudeNow = parseFloat(106.105104103); this.latitudeNow = parseFloat(-123.123); // other code... }; mymap.on("geosearch/showlocation", function (result) { console.log(result); // Remove marker ...

Verify whether the input from ng-model is numeric

When utilizing ng-model binding in an input field, data is usually stored as a string. Is there a way to verify if the user has entered a number? ...

How to implement v-tooltip with v-for in Vuetify?

I'm having trouble using the v-tooltip within a v-for loop. It seems like my binding is off, preventing it from showing up correctly. The v-tooltip works fine when used outside of the v-for loop: <div v-for="item in items"> <v-t ...

The message "Error: Unknown custom element: <router-view> - have you properly registered the component?" is prompting for a solution

Even though the name is correctly capitalized in all my component instances, I am still encountering an error. After researching similar issues online, it appears that the problem usually revolves around naming discrepancies. However, I have double-checked ...

When the value remains unchanged, Angular's @Input() value does not get updated

Upon running this program, I initially receive the output as false, 5, 500 since they have been initialized in the child component. However, upon clicking the update button, I am unable to revert to the previous values. Instead of getting true, 10, 1000, I ...

What is the best way to send a file to a JavaScript function within Django?

I am in the process of integrating the PhylD3 library into a Django Template. The CSS and JS files have been integrated into the Django static directory. However, I am unsure about the most efficient way to load the XML file in the template. Template - ...

Encountering an issue in Angular 8 where there is a difficulty in reading the property 'NODE_NDEBUG' when attempting to serve the application

An issue has been identified in the assert-plus library at ../node_modules/assert-plus/assert.js where it is encountering difficulties reading 'NODE_NDEBUG' from 'process.env', as highlighted in the code snippet below module.exports = ...

AngularJS Infowindow Button

Within my map, I have designated various locations with markers. Once a marker is clicked, an infowindow appears containing the place's name and a button. To capture these places in an array when the button within the infowindow is clicked, I require ...