Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Danilo-Araujo-Silva/be42e0d5ab720825c8f3d6caaec7fb7d to your computer and use it in GitHub Desktop.
Save Danilo-Araujo-Silva/be42e0d5ab720825c8f3d6caaec7fb7d to your computer and use it in GitHub Desktop.
A strategy to validate child components with Vue and Vee Validate.
// Somewhere in the initialization:
import VeeValidate from "vee-validate";
Vue.use(VeeValidate);
// Then, in the parent component:
export default {
provide () {
return { parentValidator: this.$validator }
},
...
}
// Then, in the child component:
export default { // or `export default Vue.extend({`
inject: ["parentValidator"],
...
created() {
this.$validator = this.parentValidator
}
...
}
// Then, in the component to be validated (example):
<v-autocomplete
name="myName"
v-validate="'required'"
:error-messages="errors.collect('myName')"
...
>
</v-autocomplete>
@kevinbazira
Copy link

kevinbazira commented Mar 25, 2023

Thanks for sharing this @Danilo-Araujo-Silva.

For those using VeeValidate version ^3.0.0, the above initialization will throw an error. (see reported issue)

The solution is to downgrade to VeeValidate version 2 based on the responses of the VeeValidate creator in the reported issue above. (v2.1.5 worked for me)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment