Creating dynamic data fields in a Vue instance for form validation in Vue2

As a newcomer to Vue.js, I'm tackling the challenge of implementing form validation on a dynamically generated form. The input fields are populated based on the JSON object retrieved through an AJAX request. While exploring various form validation libraries, I noticed that most of them require a static data property within the Vue instance for validation to function correctly. For example:

...
data() {
  return {
    name: '',
    age: '',
    ...
  }
}
...

However, since the form fields are contingent on the JSON object, hardcoding these values in the data section is not a viable option. One approach I considered was iterating through the JSON object, extracting a unique key value (like the field name), and pairing it with an empty value. Then, I aimed to store this object in a data variable to pass into the data field in the Vue instance. Yet, this method relies on each input field having a distinct name (considering scenarios where multiple fields have the same name, such as "First Name" and all are mandatory).

Is this strategy on the right track? Could I be overlooking a fundamental concept? Kindly advise if additional information is necessary! Thank you.

My setup: The main.js file instantiates the Vue instance and references App.js, which serves as a component. App.js houses the data field and calls upon various components (e.g., input fields, radio buttons, checkboxes) depending on their presence in the JSON object.

Answer №1

If you want to set data in Vue, you can use the Vue.set method:

onload: function(data) {
    for(var i in data) {
        Vue.set(this, i, data[i]);
    }
}

For more information, check out the API Documentation

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

The Android system does not support the conversion of java.lang.String to JSONObject

I am currently working on building an Android application that allows users to register a profile using JSON Parser with web hosting. I have successfully implemented the login function, but encountered an error message stating that the return object from t ...

Retrieving HTML content from Wikipedia's API using JavaScript

I am encountering an issue where every time I attempt to log data to my console, an error occurs. Could someone kindly point out what may be the problem with the code? My objective is to actually showcase the html content on a webpage. Below is the code ...

Tips for refreshing a webpage after switching screens

// I have implemented the code below to navigate from one screen to another, specifically the Home page. However, I am facing issues with the page refreshing or reloading when navigating to the home screen. None of the lifecycle methods are being called, e ...

Sending a delete request mutation using local storage: a step-by-step guide

I'm currently developing a recipe application and facing an issue with the delete button functionality. Instead of deleting the selected recipe from Vuex with local storage, it deletes the most recent one added to the Vuex array. Below is the code sn ...

Switch between light and dark mode on a webpage using HTML

I am looking to create a script using JavaScript, HTML, and CSS that can toggle between dark mode and night mode. Unfortunately, I am unsure of how to proceed with this task. https://i.sstatic.net/xA3Gq.png https://i.sstatic.net/v5vq5.png My goal is to ...

The concatenation function in JavaScript does not seem to be functioning properly with JSON

My attempt to use .concat() in order to combine two objects is resulting in tiles.concat is not a function The following code (in an angular app and written in coffeescript): $scope.tiles = new UI(); $scope.tiles.loadUITiles(); console.log($sco ...

How to eliminate space preceding a div using jQuery?

I am trying to modify the following code snippet: <div id="one">1</div> <div id="two">2</div> <div id="three">3</div> Is there a method for me to eliminate the space before div "three" in order to achieve this result: ...

How can I toggle input disablement in Bootstrap depending on switch selection?

I have been exploring the Bootstrap 5 documentation in search of a way to disable other input fields when a toggle switch input is set to 'off'. The parent element for this scenario is #member_form, and the switch itself is identified as 'to ...

Using the filter function in JavaScript to retrieve specific data

When the user_id from two different data sources matches, I need to return the email from the first source. The first source is a table in PostgreSQL called "results" and the second source is JSON data. I attempted this code snippet: var table = db.query ...

Is the NodeJS debugger prone to crashing when utilizing `this` to invoke an unidentified function?

My JavaScript file contains the code below: (function (context) { console.log(123); debugger; })(this); When I run my script in debug mode like this: $ node debug script.js I noticed that the other lines in debug mode are not colored green. Wha ...

The ID update functionality in Node.js is malfunctioning

Hello everyone, I am currently venturing into the world of NodeJS with a goal to create a backend API for a car rental agency. After writing some code to update, view, and delete records by id stored in MongoDB, I encountered a strange issue where it only ...

Enhancing the visual appearance of a date in Vue when filling input fields

How can I format a date in an input box populated with Vue from an object? This is the example code showcasing how the input box is populated using :vue <input type="text" id="Example" :value="v.date" spellcheck="false"> The date stored in the obj ...

Is it possible to switch out all instances of "GET" methods with "POST" throughout the codebase?

While working on a web application, we encountered caching issues with Internet Explorer (caching occurred due to the use of GET requests). The problem was resolved when users turned on "Always refresh from server" in IE's Developers Tool. Although we ...

The relentless Livewire Event Listener in JavaScript keeps on running without pausing

I need to create a solution where JavaScript listens for an event emitted by Livewire and performs a specific action. Currently, the JavaScript code is able to listen to the Livewire event, but it keeps executing continuously instead of just once per event ...

Attempting to employ the .reduce method on an object

Currently, I am faced with the task of summing a nested value for all objects within an object. The structure of my object is as follows: const json = [ { "other_sum": "1", "summary": { "calculations" ...

Is it possible to locate/create a JSON keyname in a jQuery $.post function?

Currently, I am diving back into the world of Ajax after a long break. At this point, I have created a page that is able to fetch a dynamic number of elements (initially ten, but this can change), and populates each of the ten divs with relevant text. In ...

Retrieving data from JavaScript global variables within an HTML document using PhantomJS

Recently, I encountered an interesting piece of HTML code that sparked my curiosity: <html> <body> <script> var foo = { bar: [] }; </script> </body> </html> This led me to wonder how I c ...

Guide to taking a screenshot of an HTML page using Javascript, Jquery, and HTML5 functionality

I am looking to add a unique feature to my website - the ability to capture the current image of my webpage with just the click of a button. Inspired by Google's "send feedback" function, I want to implement something similar on my own site. After res ...

Navigate to the element by clicking on the link, then apply a class to that specific element

I'm currently working with this HTML code: <div id="main"> <p>some text</p> <a id="goto-notes" href="#">see notes</a> <p>some text...</p> <div id="notes"> <p>notes here< ...

React testing with Mocha experiencing an Invariant violation

Currently, I am in the process of setting up a React project using Electron. Lately, I have been experimenting with configuring Mocha as my test framework. Everything seems to be working fine when I run: "test": "mocha -w --require babel-core/register ...