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 2 / Vue composition API example.

See original GitHub issue

I tried to get up and running with vuelidate 2 and the vue composition api.

I got as far as setting it up, and I have the validation model being returned, but its not totally working, the model updated but $dirty and $valid did not update.

Could we please get an example of how it should work?

Here is what i tried:

<script>
import { ref, reactive } from '@vue/composition-api'
import { email, required } from '@vuelidate/validators'
import useVuelidate from '@vuelidate/core'
import useLogin from '@/composables/auth/useLogin'
export default {
  name: 'LoginForm',
  setup() {
    // Wrapping this in reactive since the plugin has a bug with .value
    const loginService = reactive(useLogin())
    const username = ref()
    const password = ref()
    const submit = () => {
      loginService.login(username.value, password.value)
    }

    let validation = useVuelidate(
      {
        username: { required, email },
        password: { required }
      },
      { username, password }
    )

    return {
      username,
      password,
      submit,
      loginService,
      validation
    }
  }
}
</script>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
javiercbkcommented, Jan 8, 2020

hey @blowsie, can you post the full solution? I’m currently experiencing the same issue but I’m not sure what am I doing wrong.

Thanks a lot @blowsie, I wouldn’t have figured out so quickly without your example

Here is my vue file

<template>
  <div class="container">
    <div class="columns is-centered">
      <div class="column is-5-tablet is-4-desktop is-3-widescreen">
        <form action="" class="box" @submit="login" novalidate>
          <div class="field">
            <label for="" class="label">Email</label>
            <div class="control has-icons-left">
              <input
                type="email"
                v-model="email"
                class="input"
                :class="{ 'is-danger': $v.email.$invalid && $v.email.$dirty }"
              />
              <span class="icon is-small is-left">
                <b-icon pack="fas" icon="envelope"> </b-icon>
              </span>
            </div>
          </div>
          <div class="field">
            <label for="" class="label">Password</label>
            <div class="control has-icons-left">
              <input
                type="password"
                v-model="password"
                class="input"
                :class="{
                  'is-danger': $v.password.$invalid && $v.email.$invalid
                }"
              />
              <span class="icon is-small is-left">
                <b-icon pack="fas" icon="lock"> </b-icon>
              </span>
            </div>
          </div>
          <div class="field">
            <button
              class="button is-success"
              :disabled="$v.$invalid && $v.$dirty"
            >
              Login
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" src="./login-form.ts"></script>

and my typescript file

import { createComponent, ref } from "@vue/composition-api";
import { useState } from "@u3u/vue-hooks";
// prevent typescript complaining about no typings
const { email, required } = require("@vuelidate/validators");
const useVuelidate = require("@vuelidate/core").default;
import store from "@/store";
import router from "@/router";
import { AuthCredentials } from "@/store/login";

export default createComponent({
  setup() {
    const storeState = {
      ...useState("login", ["loading", "error"])
    };

    // you must use ref, reactive is not supported
    // I believe the reason is https://github.com/vuelidate/vuelidate/blob/next/packages/vuelidate/src/core.js#L392
    const email = ref("");
    const password = ref("");

    const $v = useVuelidate(
      {
        email: { required, email, $autoDirty: true },
        password: { required, $autoDirty: true }
      },
      { email, password }
    );

    const login = function(e: Event) {
      e.preventDefault();
      if (!$v.$invalid) {
        const credentials: AuthCredentials = {
          email: email.value,
          password: password.value,
          onLoginSuccess: () => {
            let nextRoute = store.getters["session/savedRoute"];
            if (nextRoute) {
              nextRoute = { name: "events" };
            }
            router.replace(nextRoute);
            store.dispatch("session/clearSavedRoute");
          }
        };
        store.dispatch("login/login", credentials);
      }
    };

    return {
      ...storeState,
      email,
      password,
      login,
      $v
    };
  }
});

2reactions
shentaocommented, Jun 4, 2020

Well, in that case, @ansarizafar keep your fingers crossed for this weekend! 🤞

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vuelidate 2 - Composition API + Vue 3 - CodeSandbox
Template Info. Vuelidate 2 - Composition API + Vue 3. vue 3. validation. vuelidate ... App.vue. main.js .gitignore. README.md. babel.config.js. package.json.
Read more >
Vuelidate: Getting started
Vuelidate 2 is a simple, but powerful, lightweight model-based validation for Vue.js 3 and 2. Vuelidate is considered model-based because the validation ...
Read more >
A Guide To Vue 3 Form Validation - Vuelidate Tutorial
Vuelidate provides model-based validation for Vue projects. It's a simple, yet powerful way to add form validation into your projects because it ...
Read more >
Vuelidate - A Lightweight Model-based Validation for Vue.js
To use Vuelidate with the Composition API, you need to provide it a state and set of validation rules, for that state. The...
Read more >
Compose validation logics with Vue 3.0 and Vuelidate. - ITNEXT
The new vuelidate is implemented using Vue 3.0 composition api. It looks like below. One point is that you can not use reactive...
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