What is the best way to verify changing input fields in vue.js?

Validation of input fields using vuelidate is essential. The input field in question is dynamic, as the value is populated dynamically with jsonData through the use of v-model.

The objective:

Upon blur, the goal is to display an error if there is one; however, when typing inside the input field, no errors are shown.

My current approach involves the following for the input field:

    <div v-for="data in displayProfileData" :key="data.email" >
     <p>{{data}}</p>
   <div class="row">
       <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-3">
                        <label for="phoneNo">Name</label>
              <input v-model="data.businessname"
              @blur="$v.form.name.$touch()"
              type="text" 
                            class="form-control" name="name"
                            id="name">
             <div v-if="$v.form.name.$error" class="form-error">
               <span v-if="!$v.form.name.required" class="text-danger">nameis required</span>
             </div>
                    </div>
          <p>{{$v}}</p>
   </div>
     </div>

To monitor changes, I display $v on the UI, but unfortunately, no modifications are detected when typing in the input field.

Regarding my script code:

    <script>
import { required,minLength } from 'vuelidate/lib/validators'
import axios from '../../services/base-api'
export default {
    data (){
      return{
          form :{
        name:''
          },
           displayProfileData:[]
      }
  },
  validations: {
    form: {
      name:{required}, 
    }
  },
  created(){
        this.userId = localStorage.getItem('user-Id')
        axios().post('/api/v1/Profile/getProfileData',this.userId)
        .then(res=>{
                console.log(res.data)
                this.displayProfileData=res.data

})
.catch(err=>{
    this.$toasted.error(err,{duration:2000})
})

}
}
</script>

The server provides data in the format

{ "businessid": "8126815643", "businessname": "manish",}

Issue: Initially, when the page loads, the input field displays manish. However, upon changing it and focusing out, an error message stating name is required appears. The cause of this issue remains unclear.

2:Dynamic Form- Check Here

Please review https://i.stack.imgur.com/OLRF7.png

Answer №1

As per the guidelines provided in the vuelidate documentation, it is recommended to implement the following updates:

<div v-for="data in $v.displayProfileData.$each.$iter" :key="data.email">
  ...
  <input  v-model="data.businessname.$model"
          @blur="data.businessname.$touch()"
          type="text"
          class="form-control"
          name="name"
          id="name"
  >
  <div v-if="data.businessname.$error" class="form-error">
     <span v-if="!data.businessname.required" class="text-danger">name is required</span>
  </div>
...
</div>
 validations: {
    displayProfileData: {
      //required,
      //minLength: minLength(1),
      $each: {
        businessname: { required }
      }
    }
 }

Here is the link to my codesandbox example for reference: sandbox link

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

Using Ember's transitionTo method and setting a controller attribute within a callback

I am working with Ember code to transition to a specific route while also setting controllerAttr1 on 'my.route' Upon transitioning to "my.route" using this.get('router').transitionTo("my.route"), I have set up a callback function that ...

Using Vue 3, Bootstrap, and Pinia to create an innovative Global Modal experience

After creating a ModalComponent.vue file that I intend to use across different sections, I encountered an issue with closing the modal after calling my Pinia stores. The modal includes slots for the title, body, and footer, along with a standard close butt ...

"Encountering a RangeError with Node.js Sequelize while working with

I need some assistance with using Sequelize in my Node.js application paired with MySQL. When I try to execute the command npx sequelize db:create, I encounter an error that I do not know how to resolve. Any guidance on this matter would be greatly appreci ...

Discovering the solution to populating and building a tree structure using jsTree in conjunction with SQL Server, addressing the challenges associated with the

My current challenge involves using JSTREE to display a list of system modules. The issue arises from the fact that, according to the jsTree documentation, I need to use # in my query to create the tree structure. However, when I execute the following quer ...

Changing an element in an array by using a specific input

With the usage of either JavaScript or Jquery, I possess an array that has been arranged in accordance with Start Date (coordinates): [{ elem: '<div id="task7">', startDate: 864, endDate: 999, order: 0 }, { elem: '<div id ...

What is the method for obtaining the worldwide location of a vertex on a skinned mesh in Three.js?

In the realm of Three.js, we've recently acquired the ability to determine the global position of a vertex in a non-skinned mesh thanks to insights from this question. Now, the query arises - how can one ascertain the global position of a vertex in a ...

Perform a POST request in JavaScript and handle the response in PHP

Currently, I am in the process of gathering data through a form and executing this JavaScript: fetch('process.php', { method: 'POST', body: formData, }).then(response => alert( response ) ); The execution of process.php hap ...

Ways to continuously monitor a div for a specific class

Is there a way to continuously check if an area has the class "top-nav" in order for the alerts to work every time it lacks or contains the class? How can I achieve this functionality? Check out the code on jsfiddle: https://jsfiddle.net/jzhang172/117mg0y ...

interactive form fields updating using javascript

Hey there! I'm a beginner in web development and currently facing a challenge. I have a form with 2 fields generated by one drop-down menu. I've managed to generate one field, but struggling to get the other due to my limited knowledge. I just ne ...

Unspecified data output from jquery datepicker

I am currently utilizing a datepicker to select a date. I have implemented the jQuery datepicker, however, it is returning an undefined value. Can someone please assist me with this issue? My HTML CODE: <input type="text" class="form-control fromdate" ...

Iterate over the key-value pairs in a loop

How can I iterate through a key-value pair array? This is how I declare mine: products!: {[key: string] : ProductDTO}[]; Here's my loop: for (let product of this.products) { category.products.push((product as ProductDTO).serialize()); } However, ...

Utilize the power of XMLHttpRequest to fetch and load numerous audio files, seamlessly integrating them for playback through the Web Audio

I am looking to create a web application that loads three different audio files, each one second long, in a specific order, and then merges them into a single Audio Buffer consecutively. To illustrate my goal, here is a sample code snippet: var AudioCo ...

The vuex module could not be located

I am just starting out with Vue and I am using Vue CLI. Below is the code I have written: App.vue: <template> <div id="App"> <Count/> </div> </template> <script> import Count from './components/Count&apo ...

Unable to toggle Bootstrap 5 tabs in a Nunjucks template - the issue persists

I have been following the bootstrap documentation for tabs which can be found at this link After referencing the documentation, I replicated the sample implementation in my code as shown below: --- title: Portfolio description: Portfolio --- {% exten ...

Manipulating Strings in JavaScript Arrays

I am working with an array of arrays of data that is being retrieved from a csv file. My goal is to filter out specific indexes of an array based on the titles they contain. For instance: If an array index includes the title "Retail", I want to return the ...

Why is my Laravel function retrieving outdated session data?

Currently, I am in the process of developing a shopping cart feature using session. My goal is to create a function that can update the quantity of an item in the shopping cart and refresh both the subtotal and the list of items in the cart displayed on th ...

Problem with Material UI Checkbox failing to switch states

I'm a bit confused about the functionality of my checkbox in Material UI. The documentation makes it seem simple, but I'm struggling to get my checkbox to toggle on or off after creating the component. const createCheckBox = (row, checkBoxStatus, ...

Is it possible to encrypt data using a private key in Angular and then decrypt it using a public key in PHP using RSA encryption?

Attempting to Encrypt data using the RSA Public Key in Angular and Decrypt it with the public key in PHP. Encryption is done with the JsEncrypt library in Angular, while decryption takes place in PHP. :openssl_public_decrypt($signature, $decrypted, $public ...

What is the best way to implement a scroll to top/bottom button globally in Vue?

I have created a scroll-to-top and scroll-to-bottom button for my application. However, I want to make these buttons accessible globally throughout the app without having to repeat the code in every page where the component is needed. Currently, I am inclu ...

Using JavaScript, what is the best way to retrieve the provided JSON data?

Is there a way to navigate through the JSON structure displayed below? { "question":{ "119380":{ "email":{ "-L8o-zzRRTOJQjI4BQZ0":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4c5c6c7e4c3c9c5cd ...