The issue of HandleSubmit not functioning properly when used in conjunction with vee-validate on Vue3 and V

I am facing an issue with submitting a registration form that uses vee-validator for data validation. The handleSubmit function from vee-validator seems to be not returning or logging anything.

Here is my code:

<script setup lang="ts">
import type { User } from '@/models/user';
import { useUserStore } from '@/stores/client/user';
import { ref } from 'vue';
import { useForm } from 'vee-validate';
import * as yup from 'yup';

const schema = yup.object({
    name: yup.string().required('Name is required'),
    surnames: yup.string().required('Surname is required'),
    birthdate: yup.date().nullable().required('Birthdate is required').transform((value, originalValue) => originalValue === '' ? null : value),
    email: yup.string().email('Email is not valid').required('Email is required'),
    password: yup.string().min(8, 'Password is too short - should be 8 chars minimum.').required('Password is required'),
    passwordConfirmation: yup.string()
        .oneOf([yup.ref('password'), undefined], 'Passwords must match')
        .required('Password confirmation is required'),
});

const { handleSubmit, defineField, meta, errors } = useForm({
    validationSchema: schema
});


const [name, nameAttrs] = defineField('name');
const [surnames, surnamesAttrs] = defineField('surnames');
const [email, emailAttrs] = defineField('email');
const [password, passwordAttrs] = defineField('password');
const [passwordConfirmation, passwordConfirmationAttrs] = defineField('passwordConfirmation');

const onSubmit = handleSubmit(values => {
    console.log('Handling submit:', values);
    console.log("Form submitted");
    console.log("Values:", values);
    console.log("Is form valid?", meta.value.valid);
});
</script>


<template>
    <section class="form-section">
        <div class="form-container">
            <h1 class="form-title">Create an account</h1>
            <form @submit.prevent="onSubmit" novalidate>
                <input type="text" v-model="name" v-bind="nameAttrs" placeholder="Your name" class="input-field" required>
                <span v-if="errors.name" class="error-message">{{ errors.name }}</span>

                <input type="text" v-model="surnames" v-bind="surnamesAttrs" placeholder="Your surnames" class="input-field" required>
                <span v-if="errors.surnames" class="error-message">{{ errors.surnames }}</span>

                <input type="email" v-model="email" v-bind="emailAttrs" placeholder="Your email" class="input-field" required>
                <span v-if="errors.email" class="error-message">{{ errors.email }}</span>

                <input type="password" v-model="password" v-bind="passwordAttrs" placeholder="Password" class="input-field" required>
                <span v-if="errors.password" class="error-message">{{ errors.password }}</span>

                <input type="password" v-model="passwordConfirmation" v-bind="passwordConfirmationAttrs" placeholder="Confirm password" class="input-field" required>
                <span v-if="errors.passwordConfirmation" class="error-message">{{ errors.passwordConfirmation }}</span>

                <button type="submit" class="submit-button">Create an account</button>
                <p class="link-text">Already have an account? <a href="/login" style="color: #007BFF;">Login here</a></p>
            </form>
        </div>
    </section>
</template>

The onSubmit method is supposed to print the values from the handleSubmit in the console, but it appears to be malfunctioning.

Answer №1

You need an additional pair of parentheses.

const onSubmit = handleSubmit(values => {
    console.log('Submitting form:', values);
})();

When you invoke the handleSubmit function in your form library, it doesn't trigger the submission process right away. Instead, it returns a new function that, upon execution, will carry out the form submission. This returned function is responsible for handling tasks like form validation, retrieving input values, and invoking your specified callback function with these values.

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

How can a JavaScript file interact with a backend without needing to specify the URL in the compiled code, thanks to webpack?

Currently, I am working on a React application with webpack. After compiling the code using the command webpack --mode production && webpack --config webpack.config.prod.js I utilize a .env.prod file to specify the variable REACT_APP_BASE_URL, wh ...

What is the recommended way to delete Mesh objects in ThreeJS?

What are some effective methods in ThreeJS for creating and deleting Meshes (comprised of Geometry and Materials) efficiently? I want to showcase latitude/longitude points on a revolving 3D globe. Each point should be clickable, revealing additional infor ...

Creating a toggle label using just CSS: a step-by-step guide

Recently, I was trying to create a toggle label using HTML, CSS, and JavaScript. Here is the code snippet that I experimented with: function genre_itemclick(item){ if(item.classList.contains('light_blue_border_button')) { ...

Executing a click on a checkbox element in Python with Selenium

Background: Currently, I am in the process of developing a Python script that will automatically download listings from a specific website. I have successfully programmed the script to navigate the webpage, search for keywords, click on the search button, ...

The website encountered an error in loading with the error message "ENOTFOUND" in Cypress

All my cypress tests were running smoothly until one day they all failed to visit the target site. The error message that I received was: cy.visit() failed trying to load: https://mywebsite.com/accounts/login/ We attempted to make an http request to this ...

Customizable mongoDB database collection

Is there a more efficient way to make calls to different collections based on a function parameter? I'm exploring the possibility and if it's not feasible, I'll handle it case by case. Currently, I have this code and the goal is to have a u ...

The CSS files undergo modifications when executing the command "npm run dev"

I've been working on an open-source project where I encountered a bug. Even when there are no images to display, the "Load More" button in the web browser extension still appears. To fix this, I decided to add the class `removeButton` to the button an ...

Angular5 causing all divs to have click events at once instead of individually triggered

I am a beginner when it comes to working with Angular and I have encountered an issue. I created a click event on a FAQ page in Angular 5, but the problem is that when I click on one FAQ, they all open up instead of just the targeted one. Here is my TypeS ...

Creating a dynamic two-player chess application using Django - requiring the player's chess board to automatically update whenever the opponent makes a move

I am looking for a solution where each player has their own webpage equipped with a Javascript chessboard GUI interface that allows them to click and drag pieces. The challenge is ensuring that when one player makes a move, the other player's chessboa ...

exploring div element(s) with jQuery

My HTML page contains multiple div elements in the body. I have also included buttons with associated click functions in jQuery to change the background-color of a div element based on the button pressed. However, I'm facing an issue at the 'term ...

The `replaceAll` function is malfunctioning within the Next.js (Node.js) application after deployment to the Render.com server

Everything runs smoothly on my local setup with Node 17.4.0. However, when I deploy to my production server at Render.com, which also uses Node version 17.4.0, I encounter errors in functions. I have documented this issue here: TypeError: (0 , crypto__WE ...

Steps for creating a link click animation with code are as follows:

How can I create a link click animation that triggers when the page is loaded? (function () { var index = 0; var boxes = $('.box1, .box2, .box3, .box4, .box5, .box6'); function start() { boxes.eq(index).addClass('animat ...

unexpected alteration of text sizing in mathjax within reveal.js presentations

Something strange is happening with the font size in my slides. The code for each slide is the same, but there is an unexpected change between the 3rd and 4th slide. I cannot figure out what is causing this discrepancy. Oddly enough, when I remove the tit ...

Changing the orientation of nodes in a d3.js diagram

Using d3.js to create a nodes diagram, currently displaying parent on the left and children on the right. Is it possible to reverse this direction so that children are on the left and parents on the right? Here is the renderTree function used to display t ...

After an ajax request, the Jquery hover effects are no longer functional

On my website, I have implemented ajax calls to filter product results and apply effects. However, the effects on these products seem to stop working after the ajax call is completed. Located at the bottom of the page's code is the following script: ...

SetBootstrapTooltip at the location of the mouse pointer

Is it feasible to implement a bootstrap tooltip that follows the mouse cursor in AngularJS? If not, what other options are available for achieving this functionality? ...

Vue.js and Laravel API working together to display user information along with posts

Currently, I am in the process of learning Vue.js with an API built using Laravel. The concept is quite simple: a user can create a post, and each post can have comments. While I've been successful in establishing relationships within Laravel, I' ...

Having trouble with jQuery Animate when trying to change the background-color property?

So, I was experimenting with the jQuery .animate() function and decided to change the background color of a div based on how many pixels the user scrolled. Surprisingly, it didn't work as expected. After switching to the .css() function instead, every ...

Adding a dynamic form to each row in a table: A step-by-step guide

https://i.sstatic.net/HctnU.jpg Hey there! I'm facing a challenge with dynamically generated rows in a form. I want to send only the inputs from the selected row when a checkbox is clicked. Can you help me figure this out? I tried using let rowForm ...

Transmitting information through websockets in binary form involves converting numbers into text for encoding

I have a project in which I need to transmit binary data over a websocket connection to an LED matrix. My plan is to send the binary data as a byte array, for example, creating a diagonal line from right to left on the matrix would be represented like this ...