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.

Vuelidate as Nuxt Plugin not working

See original GitHub issue

Hi everyone,

Setup: Nuxt@2.6.1 and Vuelidate@0.7.4

Problem: I’m having trouble getting Vuelidate to work with Nuxt and I’ve tried searching for hours without any success.

I’ve created the following vuelidate.js file in my plugins folder:

import Vue from "vue"
import Vuelidate from "vuelidate"
Vue.use(Vuelidate)

Then in my Nuxt.config.js, I added the following to my plugins: plugins: [someotherpackages..., { src: "~/plugins/vuelidate" }]

In my components:

<template>
  <form @submit.prevent="test">
      <input type="email" name="email" v-model.trim="$v.email.model">
      <p v-if="!$v.email.required">The email field is required!</p>
      <p v-if="!$v.email.email">The input must be a proper email!</p>
      <input type="password" name="password" v-model.trim="$v.password.model">
      <button type="submit">Submit</button>
    </form>
  </div>
</template>

<script>
import { required, minLength } from "vuelidate/lib/validators"

export default {
  name: "SignUp",
  data() {
    return {
      email: null,
      password: null,
    }
  },
  validations: {
    email: required,
    password: {
      required,
      minLength: minLength(6),
    },
  },
}

I get the following error:

[Vue warn]: Property or method "$v" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
DonNicoJscommented, Apr 18, 2019

@chanmathew HI! I made a codesandbox with latest nuxt and vuelidate that is working: https://codesandbox.io/s/53jwkw9r9k

few things:

  • on the v-model is : $v.email.$model ( note the $ before model )
  • email validator is missing
  • for consistency I made the email validations a object

Please check the component VueliForm and the plugin vuelidate.

8reactions
golcsa-renatocommented, Mar 18, 2021

Worked for me this way, hope it helps.

plugins/vuelidate.js

import Vue from 'vue'
import Vuelidate from 'vuelidate'
Vue.use(Vuelidate)

nuxt.config.js

export default {
   plugins: [
    {
      src: '~/plugins/vuelidate.js',
      mode: 'both',
    },
  ],
}

MyComponent.vue

<template>
 <input v-model="form.name" @input="$v.form.name.$touch()" />
</template>

<script>
import { required } from 'vuelidate/lib/validators'
export default {
  data() {
    return {
      form: {
        name: '',
      },
    }
  },
  validations: {
    form: {
      name: {
        required,
      },
    },
  }
}
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt 2 & Vuelidate 2 not working properly - Stack Overflow
I have installed vuelidate 2 to validate forms in my NuxtJS project. I followed instructions for installation and setup according to ...
Read more >
Plugins directory - Nuxt
The plugins directory contains your Javascript plugins that you want to run before instantiating the root Vue.js Application.
Read more >
Vuelidate | A Vue.js model validation library
Simple, lightweight model-based validation for Vue.js.
Read more >
How to create dynamic forms with custom validation in ...
In this article we'll show you how to easily set-up dynamic forms with custom validation using Storyblok, Nuxt.js, Vuelidate and TailwindCSS.
Read more >
nuxt-vuelidate-module - npm Package Health Analysis - Snyk
The npm package nuxt-vuelidate-module was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as ......
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