Lazy validation & touch not working properly?
See original GitHub issue@vuelidate/core: ^2.0.0-alpha.12, @vuelidate/validators: ^2.0.0-alpha.11,
After updating to latest Vuelidate I’m unable to use lazy validation and I’m wondering what am I doing wrong. The simplest code I’m able to write:
<template>
<form @submit.prevent="test">
<input v-model="name" type="text">
<button type="button" @click="v$.$touch()">touch</button>
<button>submit</button>
</form>
<pre>{{ name }}</pre>
<pre>dirty: {{ v$.$dirty }}</pre>
<pre>error: {{ v$.$error }}</pre>
<pre>invalid: {{ v$.$invalid }}</pre>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import useVuelidate from '@vuelidate/core'
import { required } from '@vuelidate/validators'
export default defineComponent({
name: 'Home',
setup() {
const name = ref('')
const v$ = useVuelidate(
{
name: { required },
},
{ name },
{ $lazy: true },
)
const test = () => {
v$.value.$touch()
if (!v$.value.$error) {
console.log('success')
}
}
return {v$, name, test}
},
})
</script>
After submit, I get success regardless to name value. When I click touch first, it works. When remove $lazy, it works.
Is it a bug or intended behavior?
edit: listen for form submit instead of click
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Vuelidate: validate on click, not when field touched
Show activity on this post. I'm kinda new to vuelidate, and everything works fine, except I have no clue how to run validation...
Read more >Guide | Vuelidate
Validation in Vuelidate 2 introduces the $lazy param, which will make the selected validators lazy. That means they will only be called, after...
Read more >How to implement form validation with Vuetify in a Vue.js app
Learn how to build a registration form and implement quality form validation using Veutify, a material design component framework.
Read more >Fixing Vuetify's Form Validation. Quite ... - Robert Mirabelle
Quite unfortunately, Vuetify form validation effectively arrives “broken” out of the box. Here, I'll explain the problem and suggest a solution.
Read more >Vuelidate | A Vue.js model validation library
Contextified validators; Easy to use with custom validators (e.g. Moment.js); Support for function composition; Validates different data sources: Vuex ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t understand “You are using the name instead of the v$.name.$model, which never makes it dirty”. When I log
v$.dirty
orv$.name.dirty
right after thev$.$touch()
, it’s true.Just to make sure (and for others looking for solution), after your advice, my test method should look like this:
It seems a bit strange that I’ve to use Vue’s method to use validation library, but I’m still Vue beginner, so I cannot judge.
Your advice to use
await v$.$validate()
is complicated by the fact, that it’s type definition isreadonly $validate?: () => Promise<boolean>
, so TypeScript compiler complains about undefined object:Cannot invoke an object which is possibly 'undefined'
.It’s IMHO a pity that one can’t use
v$.$validate
in this way:but I guess it has some drawbacks.
Released a fix for the types. The
$validate
is no longer optional 😃