Reputation: 47
I inherited a Vue site with a form that was experiencing heavy input lag using Vee Validate 2.x.
I migrated to 3.2.4 (vue 2.6.10) and followed the migration guide, but am only seeing the validation check my final input.
This is an amended version of the code:
<ValidationObserver ref="form">
<form @submit.prevent="submit">
<div v-for="formField in form.fields">
<ValidationProvider name="formField.label" rules="required" v-if="formField.type != 'select'" v-slot="v">
<input
:type="formField.type"
:placeholder="formField.placeholder"
v-model="formField.value"
>
<span class="text _warning" >{{ v.errors[0] }}</span>
</ValidationProvider>
<ValidationProvider name="formField.label" rules="required" v-if="formField.type == 'select'" v-slot="v">
<select v-model="formField.value" class="content_input input -select -arrow">
<option :value="null" disabled="disabled" selected="selected">Select {{formField.label}}</option>
<option v-bind:value="option.value" v-for="option in formField.options" :value="option.value" >{{option.label}}</option>
</select>
<span class="text _warning" >{{ v.errors[0] }}</span>
</ValidationProvider>
</div>
<button class="button input" type="submit">
{{form.data.submitText}}
</button>
</div>
</form>
</ValidationObserver>
import { extend } from 'vee-validate';
import { required, email } from 'vee-validate/dist/rules';
extend('email', email);
extend('required', {
...required,
message: 'This field is required'
});
export default {
...
methods: {
submit() {
this.$refs.form.validate().then(success => {
if(!success) {
return;
}
...
});
}
}
}
Upvotes: 4
Views: 1625
Reputation: 21226
A very minor mistake - the ValidationProvider
components need to be provided unique names, and you have set the attribute name="formField.label"
which sets it to the string "formField.label". What you meant to do is set it to :name="formField.label"
(note the extra colon). Once you do that it will work.
Upvotes: 6