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.

Vue 3 + vuelidate@next: $v.$error remains false

See original GitHub issue

I’m using Vue 3 and Vuelidate@next and while $v.$invalid works $v.$error and $v.$dirty always remain false even though the individual validators show true (i.e. $v.password.$error returns true).

Sorry, couldn’t figure out how to create a jsFiddle with vuelidate@next, but here is my relevant html and js:

  <input v-model.trim="$v.email.$model"/>
  <input v-model.trim="$v.password.$model"/>

  data() {
    return {
      email: '',
      password: '',
    };
  },
  validations: {
    email: { required },
    password: { required },
  },

Here is my full vue component if needed:

<template>
<div>
  <h2 class="header">Sign In</h2>
  $v.$error: {{$v.$error}}<br/>
  $v.$invalid: {{$v.$invalid}}<br/>
  $v.$dirty: {{$v.$dirty}}<br/>
  $v.$anyError: {{$v.$anyError}}<br/>
  $v.$anyInvalid: {{$v.$anyError}}<br/>
  $v.$anyDirty: {{$v.$anyDirty}}<br/>
   $v.password.$error: {{ $v.password.$error }}<br/>
  <form @submit="signIn" autocomplete="off" novalidate>
    <div class="form-group" :class="{ 'error' : $v.email.$error }">
      <label for="email">Email:</label>
      <!-- <em v-if="$v.email.$error && $v.email.email.$invalid">Invalid Email</em> -->
      <em v-if="$v.email.$error && $v.email.required.$invalid">Required</em>
      <input v-model.trim="$v.email.$model"/>
    </div>
    <div class="form-group" :class="{ 'error' : $v.password.$error }">
      <label for="password">Password:</label>
      <em v-if="$v.password.$error">Required</em>
      <input v-model.trim="$v.password.$model"/>
    </div>
    <div class="form-group buttons" >
      <button type="button" @click="cancel()">Cancel</button>
      <button class="save" type="submit" :disabled="$v.$errors.length > 0">
        Sign In
      </button>
    </div>
  </form>
</div>
</template>

<script>
import { required } from '@vuelidate/validators';

export default {
  name: 'SignIn',
  data() {
    return {
      email: '',
      password: '',
    };
  },
  validations: {
    email: { required },
    password: { required },
  },
  methods: {
    signIn(credentials) {
      this.userRepository.signIn(credentials)
        .subscribe(
          null,
          (err) => { console.$error(err, 'Error'); },
          () => this.router.navigate(['/catalog']),
        );
    },
    cancel() {
      this.router.navigate(['/']);
    },
  },
};

</script>

<style scoped>
form {
  color: #336699;
  font-size:18px;
  padding:30px;
  width: 298px;
  margin: 0 auto;
}
input {
  display: block;
  font-size:18px;
  padding-left:10px;
  width: 275px;
}
button {
  font-size: 24px;
  color: #336699;
}
button:disabled {
  color: #999999;
}
.header {
  color: #336699;
  text-align:center;
  padding-top:20px;
  margin-top:0;
}
.form-group {
  margin: 10px;
}
.buttons {
  text-align: right;
  margin-right: 0px;
}
.save {
  background-color: #CCDDFF;
  border-color: #CCDDFF;
}
em {float:right; color:#E05C65; padding-left:10px;}
.$error input, .$error select, .$error textarea {background-color:#E3C3C5;}
.$error ::-webkit-input-placeholder { color: #999; }
.$error :-moz-placeholder { color: #999; }
.$error ::-moz-placeholder { color: #999; }
.$error :ms-input-placeholder  { color: #999; }
</style>


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
aethrcommented, Oct 2, 2020

Following on from what @MauroB45 has said, I agree that $invalid only being evaluated when $dirty is true is a big change, and it makes it hard to do some things that the previous implementation made very easy.

For example, many of our forms will have the Submit button disabled while any form field is $invalid. We display a red outline on form fields that have $error = true, but not on form fields that have $invalid = true. That way a form can be $invalid on load, disabling the submit button, but the user will only see a red outline on fields they’ve interacted with. This gives the user the opportunity to attempt filling out the entire form, and they only get negative feedback after they’ve interacted with a form field, but left it in an invalid state.

I don’t disagree with evaluation being lazy, but perhaps there should be an option to trigger evaluation when validation is first set up, that doesn’t mark fields as $dirty. Alternatively, perhaps giving an option to trigger validation manually without marking the validation dirty would also suffice?

4reactions
dobromir-hristovcommented, Oct 2, 2020

I have a Todo in the src we should fix this hehe

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced usage - Vuelidate
useVuelidate returns a computed , so you need to use .value when accessing any of it's properties, like $error , $validate inside the...
Read more >
vue.js - Vuelidate reset specific field so that $error flag is false
In this Codepen example resetting the lastName field that uses a Vuetify component works - $invalid is true while $error is set to...
Read more >
Vuelidate | A Vue.js model validation library
Simple, lightweight model-based validation for Vue.js.
Read more >
A Guide To Vue 3 Form Validation - Vuelidate Tutorial
v $.validate() to check all of our inputs. And after that runs, if any fail their validation, our Vuelidate object will have an...
Read more >
Compose validation logics with Vue 3.0 and Vuelidate. - ITNEXT
To implement validation logic with vuelidate, you need to do 3 things. ... On typing something, $v.email.required becomes false and ...
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 Hashnode Post

No results found