Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue + Vee Validate 3 Trigger Manual Validation

I've almost got my form validation working - I'm using Vee Validate 3 with Vue.js. Most of the examples of Vee Validate are for version 2, so I'm struggling a bit.

The issue I have is triggering the validation when submitting the form.

If I click the text field to focus on it first, then click submit, the validation fires and I see the error message.

If however, I don't click the text field first and just click the submit button, I don't see the error message.

How can I make this work without having to focus on the text field before I click submit?

Update

Weirdly, the console shows the error TypeError: this.validate is not a function in both cases - whether the validation works or not.

<ValidationProvider rules="required" v-slot="{ validate, errors }">
  <div>
    <input type="text" rules="required">
    <p id="error">{{ errors[0] }}</p>
  </div>
</ValidationProvider>

<script>
export default {
  methods: {
    async validateField() {
      const valid = await this.validate()
    }
  }
};
</script>
like image 733
Damian Avatar asked Dec 23 '22 20:12

Damian


1 Answers

Adam pointed me in the right direction with the ValidationObserver.

This code works for me...

<ValidationObserver ref="observer" v-slot="{ invalid }" tag="form" @submit.prevent="submit()">
  <ValidationProvider rules="required" v-slot="{ errors }">
    <input type="text">
    <p id="error">{{ errors[0] }}</p>
  </ValidationProvider>
  <button @click="submit()">Submit>/button>
</ValidationObserver>

<script>
import { VslidationProvider, ValidationObserver } from 'vee-validate'
import { required } from 'vee-validate/dist/rules'

export default {
  methods: {
    async submit () {
      const valid = await this.$refs.observer.validate()
    }
  }
};
</script>
like image 148
Damian Avatar answered Jan 18 '23 01:01

Damian