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

I am interested in creating JSON data

Below is the provided json data: { "id": 0, "isLeaf": false, "name": "root", "children": [ { "id": 3, "isLeaf": false, "name": "Node 2", "pid": 0, "disabled": true }, { "id": "new5", "isLeaf": ...

Enhancing Headless UI tabs with Tailwind transitions: A step-by-step guide

Can transitions be used with headlessui tabs? I am currently utilizing @headlessui/vue <template> <TabGroup> <TabList> <Tab>Tab 1</Tab> <Tab>Tab 2</Tab> <Tab>Tab 3</Tab> < ...

Error: `target` property is not recognized on `htmlelement` type

I am attempting to retrieve the ID of a list item in a select menu but I am having trouble getting the value from it. The value should be a number. HTML File <div class="form-group mt-3"> <label class="form-label">Produc ...

Customizing the items-per-page selection in the v-data-table component with Vuetify

I have a data table component footer with a select option that I want to update the values of. Can anyone guide me on how to achieve this? Is there a way to customize the numbers in the select option? https://i.sstatic.net/I5HCA.png ...

Adaptive Table Layout that Creates a Page-breaking Design

I have a page layout with three columns: a fixed-width left-hand navigation, a responsive content column, and a fixed-width right-hand navigation. The issue arises when the content in the middle column includes HTML tables that sometimes exceed the availab ...

Stop MatDialog instance from destroying

In my application, I have a button that triggers the opening of a component in MatDialog. This component makes API calls and is destroyed when the MatDialog is closed. However, each time I open the MatDialog for the second time by clicking the button agai ...

What is the best way to configure webpack for ng build instead of ng serve?

My .NET web application is hosted in IIS and it also hosts an Angular application. This setup requires both applications to be served on the same port by IIS, primarily because they share the same session cookie. Additionally, they are integral parts of th ...

Integrating VueJs into a pre-existing .net core 5 project

I am currently working on a .net core solution that consists of 9 different projects including api, dto, data, service, etc. I now have the requirement to incorporate a project that utilizes the Vue.js framework for the frontend within this existing .net ...

Deleting the First Item from an Array in Typescript using Angular

Clicking my Button in .html <button (click)="deleteFirst()">Delete First</button> My array setup and removal function in .ts: people = [ {first: "Tom", last: "Brown"}, {first: "Ben", last: &qu ...

The image is not displaying on the page

Slider Section (Gray Part) We verified after loading, and the data was spot on. In development mode (F12), we can inspect object information (ImgURL, Text, etc.). However, it is not being displayed. Below is the template code and script code. Thank you. ...

Submitting a Vue component in a Laravel Blade file can be done by integrating the Vue component

Attempting to incorporate a custom vue component within a submit form. Below is the blade template code: <form action="/question/{{ $question->id }}" method="post"> <label for="title">Description</label> <editor-by-vue editcontent= ...

Retrieve the data from the mat-checkbox

My goal is to retrieve a value from a mat-checkbox, but the issue is that we only get boolean expression instead of the string value. Here's an example snippet of what I'm looking for: <mat-checkbox formControlName="cb2" <strong&g ...

Tips for quietly printing a PDF document in reactjs?

const pdfURL = "anotherurl.com/document.pdf"; const handleDirectPrint = (e: React.FormEvent) => { e.preventDefault(); const newWin: Window | null = window.open(pdfURL); if (newWin) { newWin.onload = () => ...

A guide to accessing an ngModel element within a reusable component

I have a specific ngModel component inside a reusable component that is not part of a form. I need to access it in order to make some changes, but when I try to do so using the code below, it returns undefined during OnInit. Can you advise me on how to pro ...

Choosing between Vue.use and Vue.component for the main Vue component

When using Vue.use, the functionality becomes global, similar to when calling Vue.component in the root Vue component like app.vue. I recently came across a sample app that utilized both methods, with multiple calls of Vue.component and Vue.use within the ...

How can one properly extend the Object class in JavaScript?

I have a scenario where I want to enhance a record (plain Javascript object) of arrays with additional properties/methods, ideally by instantiating a new class: class Dataframe extends Object { _nrow: number; _ncol: number; _identity: number[]; co ...

What is the best way to retrieve and display a PDF file through an API in VueJS?

I am looking to display a file from my API in my VueJS client. Specifically, when accessing a certain URL, the file (pdf, text, or image) should open if the browser supports it (similar to how Chrome opens PDFs). I want to achieve this using VueJS or just ...

Automate the process of replacing imports in Jest automatically

Currently, I am in the process of setting up a testbench for a more intricate application. One challenge we are facing is that our app needs to call backend code which must be mocked to ensure the testbench runs efficiently. To address this issue, we utili ...

Encountering an error when attempting to access an object property dynamically by using a passed down prop as a variable in Vue 2 & Vuex

I have been struggling for hours to find a solution to this problem, but so far I've had no luck. I've looked at these two questions, but they didn't provide the answers I needed: Dynamically access object property using variable Dynamical ...

Encounter a scope.router.go error when using Vue.js with System.js

During my testing of a Vue.js-System.js app before transitioning to webpack2, I encountered an issue with the routing pattern. In the OPA Memberships component, when clicking on a link, I aim to request a Registration page from the router. To achieve this ...