How to successfully send data props from child components to parent in Vue3

I am currently working on a personal project to add to my portfolio. The project involves creating a registration site where users can input their personal data, shipping information, and then review everything before submission.

To streamline the process, I have implemented a view called Register.vue that includes a Vuetify stepper with three steps: PersonalDataForm (a separate component), ShippingForm (another separate component), and a recap section.

One challenge I'm encountering is how to disable the "next" button on the stepper until the form is completed. Additionally, I am struggling with passing props to solve this issue.

In the REGISTER VIEW:

<template>
  <v-stepper color="deep-purple-darken-1" :items="['Personal Data', 'Shipping Address', 'Recap']" class="my-5">
    <template v-slot:item.1>
      <v-card>
        <PersonalDataForm @formDataSubmitted="handlePersonalDataForm" :isFormIncomplete="personalDataFormIsIncomplete"/>
      </v-card>
    </template>

    <template v-slot:item.2>
      <v-card>
        <ShippingForm @formDataSubmitted="handleShippingForm" :isFormIncomplete="shippingFormIsIncomplete"/>
      </v-card>
    </template>

    <template v-slot:item.3>
      <v-card>
        <Recap @completed="submitData" :disabled="isFormIncomplete"/>
      </v-card>
    </template>
  </v-stepper>
</template>

The PersonalDataForm.vue contains fields for first name, last name, birth date, birth place, and fiscal code. It utilizes Vuelidate for form validation.

The ShippingForm.vue includes fields for full name, region, province, city, house number, email, and phone number. Similar to the PersonalDataForm, it uses Vuelidate for validation.

Answer №1

If your data relationships are straightforward and linear, using the emit method is ideal. However, if you have complex nested structures, emit can be cumbersome as you need to navigate through various levels of hierarchy. This is where Pinia comes in handy. Pinia acts as a centralized storage system where you can define actions and access them globally. This simplifies your workflow significantly and removes the reliance on parent-child structures.

Answer №2

To simplify the process, consider using boolean flags for each type of form. When a form submission is valid, update the corresponding flag and enable/disable the next button accordingly.

One way to achieve this is by creating an event named formDataSubmitted and triggering this event upon successful form submission. Then, in the parent component, listen for this event and update the boolean flag accordingly.

<script setup>
const emit = defineEmits(['formDataSubmitted']);

function handleSubmit() {
  ...other code

  if(formValid) {
     emit('formDataSubmitted', formData);
  }
}
</script>

For more information on how events function in Vue, check out Vue Event Emitter.

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

Emphasize the search term "angular 2"

A messenger showcases the search results according to the input provided by the user. The objective is to emphasize the searched term while displaying the outcome. The code snippets below illustrate the HTML and component utilized for this purpose. Compon ...

Unable to stop submission as a result of ajax verification

As someone new to java and jquery, I am facing a challenge with the code below: $(document).ready(function() { //Prevent SUBMIT if Session setting = On (Ajax) $('#formID').submit(function(e) { var prevent = false; $.ajax({ type: ...

I'm struggling to retrieve $wpdb data after using a PHP function to load my posts with AJAX

Hey there! I'm trying to create a cool feature where users can view post archives grouped by year. When they click on a specific year, all the posts from that year should be displayed. I've set up an AJAX call to my functions.php file, which con ...

Continuously flowing chain of replies from a series of queries using RxJS

I am exploring the world of RxJS and seeking guidance from experienced individuals. My goal is to establish a synchronized flow of responses, along with their corresponding requests, from a stream of payload data. The desired approach involves sending ea ...

Tips for disabling autofocus on textarea when it is initially created in Internet Explorer browser

By clicking a button, I can generate a new <textarea></textarea>. However, in Internet Explorer, the cursor automatically moves to the newly created text area. Is there a way to prevent this from happening? ...

Utilizing async await allows for the sequential processing of one item at a time within a For loop

Async await has me stumped, especially when it comes to processing items in an array with a 1 second delay: handleArrayProcessing() { clearTimeout(this.timer); this.timer = setTimeout(() => { for (const ...

Creating a dynamic anchor scrolling effect within a dropdown select menu

Having trouble achieving smooth scrolling with a select option element, only works with a link. Any suggestions? Check out the jsfiddle demo to see what I mean! $(function() { $('a[href*=#]:not([href=#])').click(function() { if (location. ...

I am facing an issue with Angular 14 and Webpack 5, where certain unnecessary nodejs modules seem to be hindering me from successfully serving

I have recently started working on a cutting-edge Angular 14 application. My current node version is v14.20.0, and npm version is 8.17.0. Despite my best efforts, I seem to be facing an issue with multiple nodejs dependencies being included in my project ...

Creating a triangle shape using Bootstrap to style a div element, similar to the image provided

Trying to achieve the look of the attached image with a few divs. I know I can use a background image like this: background:url(image.png) no-repeat left top; But I'm curious if there's another way to achieve this without using a background ima ...

Retrieve data from REST call to populate Material dropdown menu

I am looking to dynamically populate a dropdown menu with data retrieved from a Rest API. I attempted the following code: <Select id="country-helper"> {array.map((element) => ( <MenuItem value={element.code}>{element.country}& ...

Can we effectively manage the input for a component that is created dynamically in real-time?

Please note: I am a newcomer to ReactJS, JavaScript, and the world of front-end development in general. Hello everyone, I have made the decision to create a versatile component that can handle all the forms within my project based on a predefined templat ...

Unable to execute findOneAndUpdate as a function

I've been researching all morning and testing different solutions, but I'm still unable to resolve this issue. Every time I run the code below, I receive the error "TypeError: req.user.findOneAndUpdate is not a function": req.user.findOneAndUpd ...

When utilizing a prisma query with a callback function, it appears that try/catch blocks are being overlooked in Node.js

After referencing error handling methods from the prisma documentation, I encountered an issue with my code: try { prisma.daRevisionare.create({ data: { "idTweet": tweet.id, "testo": testotweet, url } }).then((dati) => { bo ...

Guide to crafting a custom asynchronous function in a separate file using Express JS

I have a specific function that I want to create called: my_function.js: module.exports = function(req, res, next){ var js_obj; // Do something with the JavaScript object above // Afterwards, I want to append "js_object" to the request object: req.js ...

Unable to upload gathered email to Mailchimp mailing list through Nodejs and express API

Seeking guidance on integrating data collection with Mailchimp in a Nodejs backend project. I am currently working on an email signup list page where users input their first name, last name, and email. The HTML file contains fields named firstName, lastN ...

Requesting information from a NodeJs endpoint

I have a NodeJs application that requires calling an end-point (http:\localhost:9900\get\employee - asp.net web-api) to retrieve data. What are some options available for achieving this task? Is it possible to utilize promises in this scenar ...

Enhancing Javascript Dialog Box with a Touch of Animation

Collaborating with the incredibly talented Mark Eirich has been an amazing experience. We have been working on revamping a JavaScript dialog box together, and Mark has set up something on JSFiddle that will be incredibly beneficial. However, I am seeking a ...

The latest update of MS CRM 2013 now includes a version number for WebResources that are of script

I came across an unusual issue in MS CRM 2013 that seems to be intentional, and I need some assistance in finding a workaround for it. The problem is that calling the getScript jQuery method from a WebResource is not possible. In CRM, a version string is ...

Build a Search Suggestions feature with Node Package Manager (NPM) and Model-View-Controller (M

Stepping into the exciting world of MVC core and harnessing NPM for JavaScript packages has been a learning curve. However, I've encountered an issue that requires some deliberation on the best course of action for resolution. To provide context, I ha ...

The toggle class feature of jQuery is malfunctioning when placed inside a different div

Hello everyone, I am currently working on a toggle effect on my webpage. However, I encountered an error when trying to move the button close to another part of the page. The button works fine if it is placed in one part of the HTML, but does not work if i ...