Troubleshooting Custom Error Message Display Issue with Vee-validate and Zod in Vue 3 Composition API

I'm currently experimenting with using vee-validate alongside zod in my project, employing Composition API and custom error messages.

One issue that I've encountered is that when I click into the input field and then quickly click outside of it, the default Zod error message "Required" is displayed. It only shows the desired custom error message "Business name is required" if I enter text into the input and then remove it.

How can I prevent the default error message from showing up? I want the custom error message to be displayed consistently.

For a live demonstration, you can check out this version: https://stackblitz.com/edit/nuxt-starter-qlkfhp?file=components%2FMyForm.vue

<template>
  <form class="m-6">
    <InputText v-model="businessName" v-bind="businessNameAttrs" />
    <div>{{ errors.businessName }}</div>
  </form>
</template>

<script setup lang="ts">
import { defineComponent } from 'vue';
import { z } from 'zod';

const formSchema = toTypedSchema(
  z.object({
    businessName: z.string().min(1, { message: 'Business name is required' }),
  })
);

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

const [businessName, businessNameAttrs] = defineField('businessName');
</script>

Answer №1

Utilize the required_error key for this purpose:

For instance:

const formSchema = toTypedSchema(
  z.object({
    businessName: z
      // insert required_error key here
      .string({ required_error: 'Business name is required' })
      .min(1, { message: 'Business name is required' }),
  })
);

You can also explore other keys that are available for use there which you can find here

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

Merging two arrays concurrently in Angular 7

When attempting to merge two arrays side by side, I followed the procedure below but encountered the following error: Cannot set Property "account" of undefined. This is the code in question: acs = [ { "account": "Cash In Hand", ...

How can I retrieve a child method in the Vue.js 3 Options API?

When working with Vue.js 3 using the Options API, I encountered a challenge in accessing a child component's method. Although there is a solution provided for Vue.js 2 and Vue.js 3 with Composition API on How to access to a child method from the paren ...

Having trouble getting the webpage to update after entering information?

My current project involves automating a website using selenium python. I've encountered an issue where manually entering J590 into the target textbox requires clicking or pressing tab to refresh the page, resulting in an available option in a dropdow ...

Issue: Unable to access GET request with Express and handlebars

Hello everyone, I'm just getting started with JS/Handlebars and I'm facing an issue trying to display an image from my home.hbs file in VS Code. When I start the server, this is the message I receive: https://i.sstatic.net/wUxB7.jpg Below is th ...

Is it feasible for JSON.stringify to maintain functions in its serialization?

Consider this item: x = { "key1": "xxx", "key2": function(){return this.key1} } Suppose I execute the following code: y = JSON.parse( JSON.stringify(x) ); After running the above code, y will contain { "key1": "xxx" }. Is there a way to include funct ...

PHP and AJAX enable the seamless transfer of large files to a server in chunks via remote upload. This process includes a time limit setting to ensure efficient

Over on StackOverflow, there's a thread dedicated to the topic of streaming large files to users in chunks. The answer provided includes code that demonstrates how to accomplish this. However, my focus is on figuring out a way to simply save the file ...

Utilizing Node npm to access the three.js module for front-end development

My current setup involves utilizing node npm for managing dependencies such as jquery on the front end through the method outlined below. Server (successful) app.use('/jquery', express.static(__dirname + '/node_modules/jquery/dist/')) ...

Trouble with escape sequences in regular expressions within jQuery Terminal for JavaScript

I'm experimenting with special character functionality in jQuery terminal. While I was successful in implementing the backspace functionality, I encountered an issue when trying to execute the escape functionality. Below is the code snippet I used: ...

Issue with data not being transferred to Vue component

I am currently working on a Vue component that receives an array of 'items' from its parent. These items are then categorized with two items in each category: computed: { // sort items into categories glass: function() { ...

When attempting to use the POST method with a JSON body in Postgresql, I encounter an error

Encountering an error: There seems to be an issue with a JSON token at position 197 while trying to parse it. It occurs in the following code snippet: at JSON.parse () at parse (C:\Users\hp\empServers\node_modules\body-parser&bso ...

locate a text within a script element

I am looking to extract a JavaScript script from a script tag. Below is the script tag I want to extract: <script> $(document).ready(function(){ $("#div1").click(function(){ $("#divcontent").load("ajax.content.php?p=0&cat=1"); ...

Require a more efficient strategy for iterating through lines of input

One of the challenges I'm facing with my form is that it contains 5 input lines. I need to keep any blank lines that are sandwiched between two filled lines, while removing all others. For instance, if the first line is blank, the second line contains ...

Is it possible to verify if each input is unique using the parsley validator?

As a novice, I am struggling with a task where I have 6 School Children IDs. The teacher needs to input these IDs, and while it's not vital for him to enter all of them, he must input at least one. Furthermore, if he decides to input more than one ID, ...

The error message from Object.create() indicates that the argument provided is not recognized as

Description Within my project, I am handling a JSON file, parsing it into an object, and attempting to transform it into an instance of the ProjectFile class using Object.create(). Code let tmpFileContent = fs.readFileSync(tmpPath, {encoding: 'utf- ...

Customizing the path of your nuxt.js router: A comprehensive guide

I have a software development marketing site with over 30 landing pages that I want to organize in the ./pages/landing folder. Currently, the files inside ./pages/landing can be accessed through URLs like: www.example.com/landing/python-development-expert ...

It appears that the use of "window.location" is causing the ajax post

I'm facing an issue with sending data to PHP using AJAX and redirecting to the PHP file. It seems that when I redirect to the PHP file, the data sent through AJAX is not being received. My goal is to have a button click trigger the sending of data to ...

Having an issue with the pop state function not functioning correctly

I am facing an issue with my images where clicking on a specific image changes the URL to that ID. However, I can only go back once and then the same URL is repeated every time I press the back button. Additionally, the forward button does not work at all. ...

Exploring Problems with jQuery FancyBox 2.0: Troubles with Helpers (Buttons and Thumbnails)

Despite meticulously following the instructions for the updated fancybox version 2, I am unable to activate helpers. The issue mystifies me as I cannot pinpoint where I may have made an error. Here is my current code: HTML: <div id="disegni" style="d ...

Setting up a webpack proxy for handling HTTP requests in a VueJs application with Axios

I am currently developing a VueJs application that utilizes Axios for handling http requests. Here is an example of how it is implemented: load () { let vm = this this.$http .get('/api/posts') .then(response => { // success ...

Limiting array size along with 'in' validation in Laravel - A simple guide

Is there a way to restrict the size of an array to exactly 2 items or 4 items, without allowing any other number of items? $validity = Validator::make($data, [ 'array', // 'between:2,4', this will include 3 too. but I need only ...