question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
khorskycommented, Feb 20, 2021

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 or v$.name.dirty right after the v$.$touch(), it’s true.

Just to make sure (and for others looking for solution), after your advice, my test method should look like this:

const test = async () => {
  v$.value.$touch()

  await nextTick()

  if (!v$.value.$error) {
    console.log('success')
  }
}

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 is readonly $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:

v$.$validate()
  .then(() => {
    // run on successful validation
  })
  .catch((errors) => {
    // validation fails, let's see all the errors
  })

but I guess it has some drawbacks.

0reactions
dobromir-hristovcommented, Feb 20, 2021

Released a fix for the types. The $validate is no longer optional 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found