Vee-Validate: Are flags on the field value yielding undefined results? Explained with TypeScript

The documentation states that by using a flag on the value of a field, I should be able to obtain a boolean. For example:

computed: {
    isFormDirty() {
      return Object.keys(this.fields).some(key => this.fields[key].dirty);
    }
 },

I am working on disabling the submit button for my form until all fields are valid. The structure of the input attributes is as follows:

type="text", @change="updateForm", name="surname", v-validate="'required'", v-model="form.surname", id="surname"
. All inputs are enclosed within a <form> tag.

The updateForm method looks like:

updateForm(event): void {
    this.$store.commit('updateForm', this.form)
  }

where the 'updateForm' mutation appears as follows:

updateForm(state, data) {
   state.form = data;
 },

The submit button is designed as follows:

<button type="submit" :disabled="isFormValid">Submit</button>

with isFormValid being a computed property structured as:

get isFormValid(): boolean {
    return Object.keys(this.form).some(key => this.form[key].invalid);
  }

The form data is also a computed property:

get form(): FormData {
    return this.$store.getters.getForm();
 }

The issue arises when attempting to access the values of the fields with invalid flags inside the isFormValid() function using

console.log(Object.keys(this.form).map(key => this.form[key])
or
console.log(this.$validator.fields.items.map(item => item.value)
, as it returns undefined instead of the expected boolean. Can you pinpoint the reason behind this unexpected behavior?

Answer №1

Upon further investigation and experimentation with the returned object, I discovered that the flags can only be accessed by using .flags after $validator.fields.

After executing

console.log((this.$validator.fields).map(field => field.flags.invalid))
, I obtained an array containing boolean values for all of my fields.

This is how my isFormValid() function ended up looking:

 get isFormValid(): boolean {
    const checkValidity = function(e) {
      return e === true;
    }
    return (this.$validator.fields).map(field => field.flags.invalid).some(checkValidity)
 }

Answer №2

I encountered a similar issue where the this.$validator.fields were empty during the rendering of the component. To solve this problem, you can implement the following code:

isFormClean() {
  if (this.fields) {
    return Object.keys(this.fields).some(key => this.fields[key].dirty);
  }
  return;
}

By using this solution, the issue should be resolved successfully!

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

Enhancing appearance with $refs?

Having trouble adding style to the $refs attribute. I keep getting an error message saying "Cannot set property 'cssText' of undefined." Is there a way to accomplish this task? I haven't come across anything similar to this issue before. th ...

Tips for updating components with fresh data in Next.JS without having to refresh the page

As part of my Todo-App development project, I am utilizing technologies such as Next.JS, Prisma, Typescript, and PostgreSQL. The data retrieval process involves the API folder interacting with the database through Prisma. CRUD operations on the Task table ...

Encountering difficulty when determining the total cost in the shopping cart

I am currently working on a basic shopping cart application and I am facing an issue when users add multiple quantities of the same product. The total is not being calculated correctly. Here is my current logic: Data structure for Products, product = { ...

Find the position of the object in a list

I have an array that looks something like this data: Array(3) 0: data: Object account_id: (...) address_1: (...) address_2: (...) amount: 10.00 id: 1234 ... 1: data: Object account_id: (...) address_ ...

The Angular Material Autocomplete component fails to show items upon upgrading the angular/material package to the newest version

Issue with Angular Material Autocomplete component not displaying items after updating angular/material package to the latest version. The autocomplete was functioning correctly with "@angular/material": "^2.0.0-beta.10" but encountered issues when update ...

Combine an array of arrays with its elements reversed within the same array

I am working with an array of numbers that is structured like this: const arrayOfArrays: number[][] = [[1, 2], [1, 3]]; The desired outcome is to have [[1, 2], [2, 1], [1, 3], [3, 1]]. I found a solution using the following approach: // initialize an e ...

Steps for creating a dynamic validation using a new form control

I have an item that needs to generate a form const textBox = { fontColor: 'blue', fontSize: '18', placeholder: 'email', name: 'input email', label: 'john', validation: { required: false } ...

Convert string to integer value

Is it possible to convert a literal string type (not runtime value) to its corresponding numerical type, for example '1' to 1? I am looking to restrict a variable to only being a key of an object (assuming the type of obj is precise since TypeSc ...

Managing memory usage in Nuxt and preventing leaks

Currently running on Nuxt v2.13 and Vuetify v2, along with using keep-alive in the default layout. I've encountered a memory issue as my application grew in size, requiring at least 4GB of RAM on a cloud server to function properly. After researching ...

Can someone provide guidance on utilizing the map function to iterate through intricate components in TypeScript?

I am facing a challenge while trying to iterate through a complex object containing 'inner objects'. When using the map function, I can only access one level below. How can I utilize map and TypeScript to loop through multiple levels below? Whene ...

TypeScript interface designed to capture objects containing a flexible number of values

In my possession is an object that looks like the following: { "0001": "a", "0002": "b", "0003": "c", ... } Is it possible for me to create a TypeScript interface that accurately represents this type? ...

What is the most effective way to sort a list using Angular2 pipes?

I'm currently working with TypeScript and Angular2. I've developed a custom pipe to filter a list of results, but now I need to figure out how to sort that list alphabetically or in some other specific way. Can anyone provide guidance on how to a ...

Converting and downloading CSV to XLSX directly from the front end using TypeScript and React

After successfully converting a JSON response to CSV format for download using the function below, I am now looking to achieve the same functionality but with xlsx files on the front end. The current function works well for CSV files and handles Japanese ...

Guide on creating an alias in webpack within vue-cli4?

Is there an alternative method for setting the alias in vue-cli4 since there is no webpack.base.conf.js file? Thank you! ...

Error: The function to create deep copies of objects is not working properly due to TypeError: Object(...) is not a

Encountering a TypeError: Object(...) is not a function in the following situation: To set up the state of a component with a specific Article (to be fetched from the backend in componentDidMount), I am implementing this approach // ArticlePage.tsx import ...

Mastering the utilization of API routes within the Next JS 13 App Router framework

As a newcomer to React JS and Next.js, I recently made the switch from using the Page Router API in Next.js to utilizing the new App Router introduced in Next.js 13. Previously, with the Page Router, creating a single GET request involved nesting your "JS ...

Using Parseint in a Vue.js method

For instance, let's say this.last is 5 and this.current is 60. I want the sum of this.last + this.current to be 65, not 605. I attempted using parseInt(this.last + this.current) but it did not work as expected. <button class="plus" @click="plus"&g ...

Issue: "contains method is not supported" in Ionic 2

I'm currently working on a code to validate the contents of my input field, but I've encountered an issue with using the contains function. Here's the TypeScript function I have written: checkFnameFunction(name){ if(name.contains("[a-z ...

Leverage Typescript to convert string literal types to uppercase

type x = 'first' | 'second' I am looking to create a type y that is similar to this: type y = 'FIRST' | 'SECOND' Here is what I attempted: type x = 'first' | 'second' type y = {[key in x]: key[& ...

Ways to restrict the quantity of Firebase compound indexes required for querying with various filters

I'm in the process of developing a Firestore project that includes group chats and forums where users can filter posts based on various criteria: Number of likes Presence of attachments (photos and/or files) 4 Tags (Questions, Exams, Assignments, Not ...