-
-
Save hiiightower/d17e537d6eb3cbe65ed4545ae99a69e6 to your computer and use it in GitHub Desktop.
Vee Validate - Child Component Example
This file contains hidden or 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
import Vue from 'vue'; | |
const bus = new Vue(); | |
export default bus; |
This file contains hidden or 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
<template> | |
<div> | |
<input v-validate data-rules="required" :class="{'has-error': errors.has("textInput")}" id="textInput" name="textInput" type="text"> | |
<span class="error" v-show="errors.has("textInput")">{{ errors.first("textInput") }}</span> | |
</div> | |
</template> | |
<script> | |
import { find, propEq } from 'ramda' | |
import bus from './bus' | |
export default { | |
mounted() { | |
//Listen on the bus for the parent component running validation | |
bus.$on('validate', this.onValidate) | |
//Watch for the changes to the childs error bag and pass back to the parent | |
this.$watch(() => this.errors.errors, (newValue, oldValue) => { | |
const newErrors = newValue.filter(error => | |
find(propEq('field', error.field))(oldValue) === undefined | |
) | |
const oldErrors = oldValue.filter(error => | |
find(propEq('field', error.field))(newValue) === undefined | |
) | |
bus.$emit('errors-changed', newErrors, oldErrors) | |
}) | |
}, | |
methods: { | |
onValidate() { | |
this.$validator.validateAll(); | |
if (this.errors.any()) { | |
bus.$emit('errors-changed', this.errors.errors) | |
} | |
}, | |
}, | |
beforeDestroy() { | |
//When destroying the element remove the listeners on the bus. | |
//Useful for dynaically adding and removing child components | |
bus.$emit('errors-changed', [], this.errors.errors) | |
bus.$off('validate', this.onValidate) | |
}, | |
} | |
</script> |
This file contains hidden or 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
<template> | |
<div> | |
<child-component></child-component> | |
<button :disabled="errors.any()" :click="submit()"></button> | |
</div> | |
</template> | |
<script> | |
import bus from './bus' | |
import ChildComponent from './ChildComponent' | |
export default { | |
mounted() { | |
//Emit that validation is required on the bus | |
this.$on('veeValidate', () => { | |
bus.$emit('validate'); | |
}) | |
//Listen on the bus for changers to the child components error bag and merge in/remove errors | |
bus.$on('errors-changed', (newErrors, oldErrors) => { | |
newErrors.forEach(error => { | |
if (!this.errors.has(error.field)) { | |
this.errors.add(error.field, error.msg) | |
} | |
}) | |
if (oldErrors) { | |
oldErrors.forEach(error => { | |
this.errors.remove(error.field) | |
}) | |
} | |
}) | |
}, | |
methods: { | |
submit() { | |
//On button pressed run validation | |
this.$validator.validateAll() | |
if (!this.errors.any()) { | |
//Do Sumbit | |
} | |
} | |
} | |
components: { | |
ChildComponent, | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment