Skip to content

Instantly share code, notes, and snippets.

@nekosaur
Created June 9, 2017 15:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nekosaur/1da5fed1abcd7ccbf077e86d33e94183 to your computer and use it in GitHub Desktop.
Save nekosaur/1da5fed1abcd7ccbf077e86d33e94183 to your computer and use it in GitHub Desktop.
<template>
<div>
<slot></slot>
</div>
</template>
<script>
import Vue from 'vue'
export default {
props: {
value: {
type: Boolean,
required: true
}
},
data() {
return {
errors: {}
}
},
watch: {
errors: {
handler() {
const errors = Object.keys(this.errors).reduce((err, key) => {
return err || this.errors[key]
}, false)
this.$emit('input', !errors)
},
deep: true
}
},
methods: {
validate() {
// Remove keys of any inputs that have been removed
const ids = this.inputs.map(input => input._uid)
Object.keys(this.errors).forEach((key) => {
if (!ids.includes(key))
Vue.delete(this.errors, key)
})
this.inputs.forEach((child) => {
child.validate()
})
}
},
computed: {
inputs() {
return this.$children.filter(child => child.hasOwnProperty('errors'))
}
},
mounted() {
this.$children.forEach((child) => {
Vue.set(this.errors, child._uid, true)
if (child.hasOwnProperty('errors')) {
child.$watch('errors', (errors) => {
Vue.set(this.errors, child._uid, errors.length > 0)
})
}
})
}
}
</script>
@nekosaur
Copy link
Author

nekosaur commented Jun 9, 2017

Not sure if it's working as is with the latest vuetify version

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