Instantly share code, notes, and snippets.

Embed
What would you like to do?
vuex + vuelidate
import Vue from 'vue'
import { validationMixin } from 'vuelidate'
import { required, minLength } from 'vuelidate/lib/validators'
export default function (store) {
const validator = new Vue({
mixins: [
validationMixin
],
computed: {
name () {
return store.getters['user/name']
}
},
validations: {
name: { required, minLength: minLength(3) }
}
})
const module = {
namespaced: true,
state: {
name: '',
},
getters: {
name (state) {
return state.name
},
$v (state) {
// don't expose api
return Object.assign({}, validator.$v)
}
},
actions: {
$touch () {
validator.$v.$touch()
},
name ({ commit }, value) {
commit('name', value)
}
},
mutations: {
name (state, value) {
state.name = value
}
}
}
return module;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment