Vee Verify managing input rows that adapt to change

I am currently facing an issue while trying to validate dynamically added input fields. When there is only one row of inputs for validation, everything works as expected.

When it works: https://i.sstatic.net/nNesg.png

However, the problem arises when I add multiple rows. The validation process ends up validating all rows collectively instead of individually. This situation poses a challenge.

Issue scenario example:

https://i.sstatic.net/Phvlj.png

While the documentation recommends assigning a unique id for each :key, I have implemented this suggestion but unfortunately, the problem persists.

This is how I am generating the dynamic inputs:

   <!-- Generate input fields and v-model -->
    <tr v-for="(row, rowIndex) in dataFields" :key="row.id">
      <td v-for="(fieldName, fieldNameIndex) in fieldNames" :key="fieldNameIndex">
        <!-- create first row and add valdiation -->
        <input
          type="text"
          class="input-style"
          v-model="dataFields[rowIndex][fieldName]"
          v-validate.initial="'required'"
          :name="fieldName"
        >
        <br>
        <span> errors.first(fieldName)}}</span>

To see the issue firsthand, check out this demo: https://codesandbox.io/s/vue-template-rtjj9?fontsize=14

Any suggestions on how I can ensure validation for each dynamically added row?

Answer №1

To ensure uniqueness, simply append rowIndex to the name field

<tr v-for="(row, rowIndex) in dataFields" :key="row.id">
  <td v-for="(fieldName, fieldNameIndex) in fieldNames" :key="fieldNameIndex">
    <!-- create first row and add validation -->
    <input
      type="text"
      class="input-style"
      v-model="dataFields[rowIndex][fieldName]"
      v-validate.initial="'required'"
      :name="fieldName + rowIndex"
      :data-vv-as="fieldName"
    >
    <br>
    <span @click="clicky()">{{errors.first(fieldName + rowIndex)}}</span>
  </td>
</tr>

Remember, customization of the name field in validation message can be done using data-vv-as.

Check out the demo here

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

Looping through arrays using ng-repeat

{ "employees" : [ { "name" : "XXX", "id" : "1", "Salary" : [ { "Month" : "XXXX", "Amount" : "XXXX", }, { "Month" : "XXXX", "A ...

Determining the selection status of an HTML input

I have been working on a unique calculator that calculates the Body Mass Index of individuals. It performs well when I manually input the values, utilizing jQuery to execute the calculations. However, my goal is to enable users to enter values using numbe ...

Do not remove the attribute from JavaScript objects

Based on the EcmaScript specification, there are certain object properties that are supposed to be undeletable due to the internal parameter DontDelete. For example: var y = 5 should not be able to be deleted. However, upon further investigation, it seem ...

Changing file names when uploading with multer

My attempt to change the file names to file1 and file2 ended up renaming both files as file2. HTML <input type="file" name="file1" file-model = "file1"/> <input type="file" name="file2" file-model ...

Is there a way to trigger an Angular $scope function from a hyperlink in the current or a new tab using a right

Here is the HTML code I am working with: <a href="" ng-click='redirectToEntity("B-",obj.id")'>Click me and Process function and then redirect</a> While this code successfully processes the function and redirects to the desired page ...

Can you explain the meaning behind this syntax?

Currently, I am utilizing Vuetify and I am in the process of getting the server-side controlled data tables up and running. While browsing through the documentation, I stumbled upon this code snippet here. However, I am facing some challenges in understan ...

A guide on customizing the appearance of individual items in a vue v-for loop based on specific conditions

I am currently developing a multiple choice quiz game and I want the selected answer by the user to change color, either red or green, depending on its correctness. To achieve this, I have created a variable called selected that correctly updates when the ...

Pass JavaScript variables to a PHP file with the help of AJAX

Hey there, I'm new to developing web-based applications and currently learning about AJAX. I've run into a problem while trying to make an AJAX request with user inputs as variables and fetching the same variables in a PHP file. Below is the code ...

What is the best way to trigger a function once another one has finished executing?

After opening the modal, I am creating some functions. The RefreshBirths() function requires the motherId and fatherId which are obtained from the getDictionaryMother() and getDictionaryFather() functions (these display my mothers and fathers on the page s ...

Creating a function within a module that takes in a relative file path in NodeJs

Currently, I am working on creating a function similar to NodeJS require. With this function, you can call require("./your-file") and the file ./your-file will be understood as a sibling of the calling module, eliminating the need to specify the full path. ...

How can I click on an element using either the href or span substring in Protractor?

Here are the details of the element I am trying to interact with using Protractor. <hsx-nav-menu-group hsx-html-element> == $0 <hsx-nav-menu-info> <i hsx-html-element hsx-icon> <svg> <use ...

How to initialize data in Vue.js components

I'm working on a Map.vue component and I need to update its data in Vue.js. However, I'm struggling to figure out how to do this: <template> <div> <div class="google-map" :id="mapName"> </div> </div> < ...

Adding content to an empty element will not produce the desired result

I'm trying to show each character of a string, which is stored in an array, one at a time. However, when I use threadsleep(a) with the code found here: http://jsfiddle.net/thefiddler99/re3qpuoo/, all the characters are displayed at once. There seems t ...

Issue with automatic form submission

What is the best way to automatically submit my form once the timer runs out and also when the refresh button is clicked? I have encountered an issue where every time I try to refresh the page, it prompts for user confirmation. If I click cancel, it stay ...

The inclusion of react-helmet in my Gatsby website is leading to an issue where the element type is considered invalid

Incorporating the Layout component in my app has significantly improved the styling. This is where I introduced react-helmet. This is how the component looks: import React from 'react'; import { Global, css } from '@emotion/core'; im ...

Using lambda expressions to sort through an array of objects in React

My goal is to create a delete button that removes items from a list and updates the state variable accordingly. public OnDeleteClick = (): void => { const selectionCount = this._selection.getSelectedCount(); let newArray = this.state.items; for ...

How to Move an Element Using HTML and jQuery: Removing with detach() and Reinsert

In my current website setup, I have implemented a feature where clicking on a div triggers the appearance of a sub menu below the clicked div. Now, I am attempting to detach() this sub menu, insert it after the div that was clicked, and adjust its position ...

Guide to dynamically generating MongoDB schemas with Node.js in a flexible and adaptable manner

Is it feasible to dynamically generate a table in mongodb using Mongoose schema with Node.js and Angular? Instead of explicitly creating a model in Node.js, you can use the following example: import mongoose from 'mongoose'; const Schema = mong ...

What is the best approach to adding components dynamically in AngularJS?

Can someone explain how to create a component in a controller? I already have an AngularJS module with a controller defined: let app = angular.module('testApp', []); However, when I try to load the component, it does not work as expected. app ...

What is the best way to position a Button at the bottom of a div using jQuery?

Within my HTML, I have divided into two col-md-6 sections. On the left side, there is a picture and on the right side, there is text. I am considering using jQuery .height() for this layout. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7 ...