Last active
March 25, 2023 10:13
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)