Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Vuetify form validation

I have trouble resetting vuetify validation in v-dialog.

This codepen is the simplified version of what I have.
https://codepen.io/yuukive/pen/BVqpEZ

With the code above, if I do

(Open dialog --> press SAVE button --> (validation fails) --> press CLOSE button --> open dialog again),

it is already validated when I open the dialog again...

Is it possible to reset validation before a user opens it the 2nd time?

new Vue({   el: '#app',   data: () => ({     dialog: false,     emailRules: [v => !!v || 'Name is required']   }),   methods: {     onSave() {       if (!this.$refs.form.validate()) return       dialog = false     }   } }) 
<div id="app">   <v-app id="inspire">     <v-layout row justify-center>       <v-dialog v-model="dialog" persistent max-width="500px">         <v-btn slot="activator" color="primary" dark>Open Dialog</v-btn>         <v-card>           <v-card-title>             <span class="headline">Email</span>           </v-card-title>           <v-form ref="form">             <v-card-text>               <v-container grid-list-md>                 <v-layout wrap>                   <v-flex xs12>                     <v-text-field label="Email" required :rules="emailRules"></v-text-field>                   </v-flex>                 </v-layout>               </v-container>               <small>*indicates required field</small>             </v-card-text>             <v-card-actions>               <v-spacer></v-spacer>               <v-btn color="blue darken-1" flat @click.native="dialog = false">Close</v-btn>               <v-btn color="blue darken-1" flat @click.native="onSave">Save</v-btn>             </v-card-actions>           </v-form>         </v-card>       </v-dialog>     </v-layout>   </v-app> </div> 
like image 351
sora Avatar asked Jun 27 '18 09:06

sora


People also ask

How do I reset my Vuetify form?

this.$refs.form.reset() will clear all inputs and reset their validation errors. this.$refs.form.resetValidation() will only reset input validation and not alter their state.

How do I reset my Vuelidate?

Using Vuelidate you can reset the validation errors by using this. $v. $reset() . In this Codepen example resetting the lastName field that uses a Vuetify component works - $invalid is true while $error is set to false.

What is lazy validation in Vuetify?

form.lazy-validation. From the Vuetify form API docs: If enabled, value will always be true unless there are visible validation errors. You can still call validate() to manually trigger validation. Here, value is used to represent whether the form is currently valid.


2 Answers

Example from docs uses:

this.$refs.form.reset()

Note that while reset() clears validation, it clears input as well.
You can follow this issue to see further updates on this.

So you can perhaps watch dialog value and reset the form:

watch: {     dialog() {         this.$refs.form.reset()     } } 
like image 196
Traxo Avatar answered Sep 21 '22 12:09

Traxo


resetValidation() will clear validation errors only, reset() will also clear input fields.

like image 20
ego Avatar answered Sep 20 '22 12:09

ego