Changing the information entered into a form within a subcomponent will result in all other fields being reset

I encountered an issue with my application that involves a form contained within a child component. The parent passes a prop (wizardData) to the child component. If the form is being used to input new data, the field values in the prop will be null; but if the user is reviewing or editing stored data, the field values will contain the relevant information.

The problem arises when trying to edit stored data. Even though the form fields are filled with the stored values, clicking on any field causes all values in the form to disappear and the $emit call updates the parent with null values instead.

I need help figuring out what I am doing wrong in this situation.

Thank you, Tom

Child Component:

<template>
  <div>
      <form @input="submit" class="form">
      <v-card-text>
        <v-text-field       
            model='wizardData.product'
            type="text"
            label="Name"
            box
            max="100%"
            autofocus
        ></v-text-field>
        <v-text-field
            model='wizardData.source'
            type="text"
            label="Source"
            box
        ></v-text-field>
     </v-card-text>
    </div>
</template>

<script>
  export default {
    props: {
      wizardData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        form: {
          product: null,
          source: null,
        }
      }
    },
      submit () {
        this.$emit('update', {
          data: {
            product: this.form.product,
            source: this.form.source,
          },
        })
      },
    }
  }
</script>

This is how my prop 'wizardData' looks like:

wizardData

{
  "product": "Cucumber",
  "source": "D112",
}

Answer №1

Connecting your input field models to the wizardData prop goes against Vue's principle of One-Way Data Flow.

It is recommended to initialize your component's local data by copying the prop and linking your field models to form. Here's an example:

// default form values in case they're missing from wizardData
const FORM_TEMPLATE = {
  product: null,
  source: null
}

export default {
  props: { wizardData: Object },
  data () {
    return {
      form: {...FORM_TEMPLATE, ...this.wizardData}
    }
  },
  methods: {
    submit() {
      this.$emit('update', { data: this.form })
    }
  }
}

and in your template

<v-text-field v-model="form.product" ... />

JSFiddle demo ~ https://jsfiddle.net/z04um7Lb/


If wizardData is changed externally to your component, you will need to create a watcher to track changes in the prop, for instance

watch: {
  wizardData (newData) {
    this.form = {...this.form, ...newData}
  }
}

Another approach (which might be more effective) is to use conditional rendering to prevent the form component from appearing until the data is ready, for example

<form-component :wizard-data="wizardData" v-if="dataLoaded" />

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

"Embracing Micro-frontends: Building dynamic web applications using a variety of frontend

I am currently facing a software architecture dilemma. My extensive monolithic web application is built using PHP and backbone js, but I am eager to integrate a new front-end framework like react/vue. The question arises about the best approach to tackle t ...

Tips for inserting a hyperlink on every row in Vuetables using VUE.JS

Trying to implement a link structure within each row in VUEJS Vuetables has been challenging for me. Despite my research on components and slots, I am struggling to add a link with the desired structure as shown below: <td class="text-center"> <a ...

Send the id of the chosen row to the Component tag within the blade file

I'm working on passing the id of the currently selected row within a for loop when it's clicked on. The goal is to then pass that id to a Vue component. In my index.blade file: @foreach($cats as $cat) <tr> <td class="catme" d ...

PHP and Vue component not displaying properly on webpage

I am having some trouble with my PHP code, as I am attempting to incorporate a Vue component into an HTML file. However, all I see is a blank page and there are no JavaScript errors appearing. Could someone please review the code below and let me know if ...

Redirecting in Laravel after making an AJAX request results in a "Method Not Allowed" error

I've encountered an unusual problem with my web application built using Vue 3 and Inertia, which includes forms. So, here's the scenario: If I have two browsers open and log out in one, then trigger an Ajax request in the other, it goes through t ...

Passing Parameters Using Axios in Vue.js

Currently, I am utilizing Axios within VueJS to make ajax requests. However, I have encountered an issue when attempting to send these requests. This snippet shows my JavaScript code: axios.post('http://localhost/app/php/search.php', { quer ...

The Safari browser is currently having trouble loading the data sent back from the server

After successfully developing and deploying my website carspeedyrental.com, I received reports from some clients indicating that the website does not display available cars on the iPhone Safari browser. Interestingly, when tested on various browsers on A ...

Issue with Laravel Vue search-dropdown and other components failing to render correctly for certain users

We are currently implementing a searchable dropdown menu for our web application using a vue component. Strangely, the component fails to load on my local version (xampp) as well as on the deployed website. However, it displays properly on another develope ...

Laravel has not been properly initialized

Recently, I've been exploring Laravel 5.3 and vue.js and I'm trying to make a GET request to fetch some user data from my database. I'm utilizing components in this project. Here is a snippet from my app.js file: require('./bootstrap ...

Encountered an error: Vue is not recognized within Laravel environment

While working on a Laravel application with Vue 3, I encountered an issue. When trying to load the welcome.blade.php file, I couldn't see the Vue file content and received the following error in the console: Uncaught ReferenceError: Vue is not defined ...

In my database, I have two tables: Table1 and Table2. To better illustrate my issue, I have included a picture for reference

https://i.stack.imgur.com/GQAMy.jpg I am facing a challenge with joining two tables, table1 and table2, in MySQL. I need assistance in creating the resultant table as shown in the attached picture. Your help in solving this issue would be greatly appreci ...

The Vuejs code functions properly in the overall application, but is not functioning within the specific component

Before, my Vue.js app worked flawlessly until I tried putting it into a Vue component. It started throwing the following error: [Vue warn]: Error compiling template. Here's the code for the component (components.php): <script> Vue.component(&ap ...

Here is a unique rewrite: "Strategies for effectively passing the data variable in the geturldata function within Vue.js on the HTML side vary

How can I properly pass a variable in the getdataurl function in Vue.js? I need help with passing a variable in getdataurl function in Vue.js. Please provide a clear explanation and include as much detail as possible. I have tried doing some background r ...

Please refrain from displaying the user table data in the View page source under any script tags

When I access the user data in the script tag of my app.blade.php file, the page resources show this line of code. 'window.user = {"id":2,"name":"test","email":"hidden_email@example.com","email_verified_at":null,"created_at":"2021-03-02T04:52:43.0000 ...

Enhancing Efficiency with Laravel 5: Updating Multiple Data Entries

My Simple Todo App lacks the ability to persist multiple record updates simultaneously. The client-side version can be found here: http://codepen.io/anon/pen/bVVpyN Whenever I perform an action, I send an HTTP request to my Laravel API for data persistenc ...

Changes to the folder structure in Laravel are not reflected in Vue components

Recently, while working with Laravel and Vue.js, I came across an issue after rearranging my files to upload the project online. Now, whenever I update the Vue files, the changes don't seem to reflect. Seeking assistance from the experts in this commu ...

Uploading large files on Vuejs causes the browser to crash

Whenever I try to upload a file using my browser (Chrome), everything goes smoothly for files with a size of 30mb. However, if I attempt to upload a file that is 150mb in size, the browser crashes. The server's maximum upload size has been configured ...

Is it possible to nest axios post requests within another axios post request?

Can someone assist me with getting the session ID from the API to utilize it as a part of my input for an Axios POST request in order to retrieve user information? Despite double-checking my code thoroughly, I keep encountering a session expired error. Any ...

Looking to update a component in Vue 3 and Laravel 9 without the need to reload the entire webpage

Looking for a solution to refresh the header component upon clicking the logout button, in order to display the login and register options without refreshing the entire page. Any effective suggestions on how to achieve this are greatly appreciated. The l ...

Unable to verify identity using spa

My API is running on the URL: https://example.com, and my vue.js app is running on: https://app.example.com. I have configured the .env file as follows: SESSION_DOMAIN=.example.com SANCTUM_STATEFUL_DOMAINS=https://app.example.com The axios configuration i ...