I have a form inside a modal and I'm doing the validation of the fields (client side) using the plugin vuelidate .
I use the bootstrap-vue plugin to create the modal, but when I implement the validations of vuelidate, I can not do with that the modal Cancel button clears validation errors. It closes the modal, clears the fields (with this.variavel = ''), but still shows the error text or the red indicative border and - however, if I press cancel again, the fields return to normal, without an error code.
<b-modal
id="signupFormModal"
title="Crie a sua conta!"
ref="modal"
centered
@ok="handleOk"
@cancel="clearForm"
>
<b-container fluid>
<b-card>
<b-form @submit.stop.prevent="handleSubmit">
<b-form-group
id="email"
label-for="email"
>
<b-form-input
ref="focusElement"
autocomplete="username"
id="email"
type="email"
v-model.trim="form.email"
:state="$v.form.email.$dirty ? !$v.form.email.$error : null"
@input="$v.form.email.$touch()"
required
placeholder="E-mail"
/>
<small class="error">{{emailErrors}}</small>
</b-form-group>
</b-form>
</b-card>
</b-container>
</b-modal>
My goal is to make the modal clean the fields filled with a click of the button only. The script code is below:
import { required, minLength, sameAs, email } from 'vuelidate/lib/validators'
export default {
name: 'myForm',
data: () => ({
form: {
email: '',
password: '',
confirmPassword: ''
},
show: true,
nameState: null
}),
validations: {
form: {
email: { required, email },
password: {
required,
minLength: minLength(6)
},
confirmPassword: {
required,
sameAsPassword: sameAs('password')
}
}
},
computed: {
emailErrors () {
if (!this.$v.form.email.$dirty) {
this.returnNull()
return ''
} else if (!this.$v.form.email.required) {
this.returnFalse()
return 'E-mail é obrigatório'
} else if (!this.$v.form.email.email) {
this.returnFalse()
return 'Exemplo: [email protected]'
} else {
this.returnNull()
}
}
},
methods: {
clearForm () {
/* Reset our form values */
this.form.email = ''
this.form.password = ''
this.form.confirmPassword = ''
this.$v.$reset()
},
handleOk (evt) {
// Prevent modal from closing
evt.preventDefault()
if (!this.$v.invalid) {
this.handleSubmit()
} else {
this.$v.touch()
}
},
handleSubmit () {
console.log(JSON.stringify(this.form))
this.clearForm()
this.$refs.modal.hide()
},
focusMyElement (e) {
this.$refs.focusElement.focus()
},
returnNull () {
this.nameState = null
},
returnFalse () {
this.nameState = false
},
returnTrue () {
this.nameState = null
}
}
}
Someone with knowledge in Vue, Bootstrap-Vue and Vuelidate knows how to solve the problem?
Thanks for the help right away.