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>
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.
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.
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.
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() } }
resetValidation()
will clear validation errors only, reset()
will also clear input fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With