Modal form validation with Vue and Vuelidate

Asked

Viewed 1,105 times

2

I have a form inside a modal and I am doing the validation of fields (client side) using the plugin vuelidate.

Use the plugin bootstrap-Vue to create the modal, however, when I implement the vuelidate validations, I cannot make the button Cancel modal clear validation errors. It closes the modal, clears the fields (with this.variable = '), but still shows the error text or the indicative red border and - however, if I press Cancel again, the fields return to normal without error indication.

<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 get the modal to clear the fields filled with 1 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, can tell me how to solve the problem?

Thanks in advance for the help.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.