Using the <Field> component in Vee-validate ensures that your form will always be valid when submitted

Hello all, I am currently working on a Vue 3 project and utilizing vee-validate v4 for form validation. My forms are structured as follows

<template>
  <div>
    <VForm
      v-slot="{ meta }"
      ref="form"
    >
      <VField
        v-slot="{ field, errors: nameErrors }"
        name="name"
        rules="required|min:6"
      >
        <va-input
          v-bind="field"
          v-model="account.data.name"
          class="mb-4"
          :label="$t('register.name')"
          :error="nameErrors && nameErrors.length > 0"
          :error-messages="nameErrors"
        />
      </VField>
    </VForm>
  </div>
</template>

I am using the new script setup and my code looks like this

<script lang="ts" setup>
  import { useForm } from "vee-validate";
  const form = useForm();
  const save = async function() {
    const validate = await form.validate() as any;
    if (validate.valid) {
      // My save logic
    }
  }
</script>

However, the validate.valid property always returns true, even when there are validation errors in the form. Is there a way to resolve this issue? I tried using validation schema, which did work, but it required additional code to display errors on fields or duplicating the rules within the schema. Is there a way to utilize form.validate() with the rules defined in the individual fields?

Answer №1

It is not recommended to use both the useForm Composition API and the <Form> component simultaneously. It is advised to choose one method or the other to avoid creating conflicting form contexts.

The form created by useForm() will always be valid as it does not have any fields.

To utilize the form context generated by the <VForm> component, simply replace const form = useForm(); with const form = ref()

Alternatively, you can follow a well-documented approach for form submission in the documentation - eliminating the need for template refs or manual calls to validate(), as Vee-Validate will trigger your submit handler only if the form is valid...

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

Combining enums and functions

I have found the concept of combining enums with namespaces to be incredibly valuable. For instance, consider the following: enum Status : { OK = 1, NOT_OK = 2, } namespace Status { function Color(status : Status) { if(status == Sta ...

Unable to perform a default import in Angular 9 version

I made adjustments to tsconfig.json by adding the following properties: "esModuleInterop": true, "allowSyntheticDefaultImports": true, This was done in order to successfully import an npm package using import * as ms from "ms"; Despite these changes, I ...

Using TypeScript with Next.js getStaticProps causes errors

Currently, I am grappling with utilizing getStaticProps along with TypeScript. Initially, I attempted to achieve this using a function declaration: import { Movie } from './movies/movie' import { GetStaticProps } from 'next' export asy ...

Discover how to validate a property within an array of objects and add the accurate values to a fresh array using TypeScript

I have a list of objects and I want to create a new array that contains only the objects with the 'read' property set to true. I've tried a couple of different methods, but I keep getting an error: Uncaught TypeError: Cannot read properties ...

No bugs appeared in Next.js

I am currently in the process of migrating a large create-react-app project to NextJS. To do this, I started a new Next project using create-next-app and am transferring all the files over manually. The main part of my page requires client-side rendering f ...

Error in Typescript for the prop types of a stateless React component

When reviewing my project, I came across the following lines of code that are causing a Typescript error: export const MaskedField = asField(({ fieldState, fieldApi, ...props }) => { const {value} = fieldState; const {setValue, set ...

Obtain multiple class instances through HTTP-Get in Angular

Initially, explaining this with my actual code can be confusing, so I'll simplify the issue using a smaller example. Imagine my project retrieves data from 2 tables on the server, employeeDetails and employeeNames. employeeNames: This table consists ...

In TypeScript, if all the keys in an interface are optional, then not reporting an error when an unexpected field is provided

Why doesn't TypeScript report an error when an unexpected field is provided in an interface where all keys are optional? Here is the code snippet: // This is an interface which all the key is optional interface AxiosRequestConfig { url?: string; m ...

Issue with typings in TypeScript is not being resolved

I have integrated this library into my code Library Link I have added typings for it in my project as follows Typings Link I have included it in my .ts file like this import accounting from "accounting"; I can locate the typings under /node_modules ...

What is the process for accessing an API that requires a token using Axios in a Vue.js application?

I am trying to retrieve information from an API that has a JSON file using Axios, but it requires a Token and I am unsure of how to properly implement it. Can anyone provide assistance with this? The API can be accessed here: ‫‪https://api.nytimes.com ...

Tips for resolving the unmounted component issue in React hooks

Any suggestions on resolving this issue: Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect ...

"React's FC generic is one of its most versatile features

I'm currently working on a component that can render either a router Link or a Button based on the provided props. Here is the code snippet I have: import React from 'react'; import Button from '@material-ui/core/Button'; import { ...

There seems to be an issue with my React application that was built using Webpack 5 and compiled with TypeScript. The @tailwind directive is not functioning properly in the browser, and

As I embark on creating a fresh react application using Webpack 5, Tailwind CSS, and Typescript, I find myself at a crossroads. Despite piecing together various tutorials, I am struggling to configure the postcss-loader for Tailwind. While traditional .css ...

The ngx-datatable encountered a resolution issue with its dependency tree and was unable to resolve it

I've been trying to incorporate ngx-datatables into an Angular 12 project by running the command npm install @swimlane/ngx-datatable. However, after installation, I encountered the following Errors: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to r ...

Tips on Showing a Unique List in Mat-Table?

Here's what I'm trying to accomplish: I have a list and I want to display it without any duplicates. I attempted using the code (this.model.map(x => x.map), but it resulted in an error. Can anyone help me fix this? model: myModel[]; myObj:any; ...

Guide to incorporating the useEffect hook within a React Native View

I am trying to use useEffect within a return statement (inside a Text element nested inside multiple View elements), and my understanding is that I need to use "{...}" syntax to indicate that the code written is actual JavaScript. However, when I implement ...

Angular is throwing an error about an undefined JSON object, even though I am able to access it

I have searched extensively for a solution to my error, but I couldn't find anything that matches exactly. I have tried solutions for similar errors, but they have not worked either. The JSON data is structured like this: [ { "somedata": "value ...

Experience a new way to log in with signInWithPopup, leaving the Email

I am currently working on developing a registration form that utilizes Firebase auth for authentication. There are two methods available for registering users: Using Email / Password Provider Using Google Auth Provider Here is the process I follow: Ste ...

Enhance your TypeScript code using decorators with inheritance

Exploring the realm of Typescript decorators has led me to discover their intriguing behavior when combined with class inheritance. Consider the following scenario: class A { @f() propA; } class B extends A { @f() propB; } class C exten ...

Encountered an error while trying to load config.ts file because of an issue

Trying to set up a new protractor project to conduct tests on an angular site. Node.js, typescript, protractor, and jasmine are all installed globally. After running webdriver-manager update and webdriver-manager start in the project folder, I proceed to b ...